FIRMessagingPubSub.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. @class FIRMessagingTokenManager;
  19. /**
  20. * FIRMessagingPubSub provides a publish-subscribe model for sending FIRMessaging topic messages.
  21. *
  22. * An app can subscribe to different topics defined by the
  23. * developer. The app server can then send messages to the subscribed devices
  24. * without having to maintain topic-subscribers mapping. Topics do not
  25. * need to be explicitly created before subscribing or publishing—they
  26. * are automatically created when publishing or subscribing.
  27. *
  28. * Messages published to the topic will be received as regular FIRMessaging messages
  29. * with `"from"` set to `"/topics/myTopic"`.
  30. *
  31. * Only topic names that match the pattern `"/topics/[a-zA-Z0-9-_.~%]{1,900}"`
  32. * are allowed for subscribing and publishing.
  33. */
  34. @interface FIRMessagingPubSub : NSObject
  35. - (instancetype)initWithTokenManager:(FIRMessagingTokenManager *)tokenManager;
  36. /**
  37. * Subscribes an app instance to a topic, enabling it to receive messages
  38. * sent to that topic.
  39. *
  40. * This is an asynchronous call. If subscription fails, FIRMessaging
  41. * invokes the completion callback with the appropriate error.
  42. *
  43. * @see FIRMessagingPubSub unsubscribeWithToken:topic:handler:
  44. *
  45. * @param token The registration token as received from the InstanceID
  46. * library for a given `authorizedEntity` and "gcm" scope.
  47. * @param topic The topic to subscribe to. Should be of the form
  48. * `"/topics/<topic-name>"`.
  49. * @param options Unused parameter, please pass nil or empty dictionary.
  50. * @param handler The callback handler invoked when the subscribe call
  51. * ends. In case of success, a nil error is returned. Otherwise,
  52. * an appropriate error object is returned.
  53. * @discussion This method is thread-safe. However, it is not guaranteed to
  54. * return on the main thread.
  55. */
  56. - (void)subscribeWithToken:(NSString *)token
  57. topic:(NSString *)topic
  58. options:(nullable NSDictionary *)options
  59. handler:(FIRMessagingTopicOperationCompletion)handler;
  60. /**
  61. * Unsubscribes an app instance from a topic, stopping it from receiving
  62. * any further messages sent to that topic.
  63. *
  64. * This is an asynchronous call. If the attempt to unsubscribe fails,
  65. * we invoke the `completion` callback passed in with an appropriate error.
  66. *
  67. * @param token The token used to subscribe to this topic.
  68. * @param topic The topic to unsubscribe from. Should be of the form
  69. * `"/topics/<topic-name>"`.
  70. * @param options Unused parameter, please pass nil or empty dictionary.
  71. * @param handler The handler that is invoked once the unsubscribe call ends.
  72. * In case of success, nil error is returned. Otherwise, an
  73. * appropriate error object is returned.
  74. * @discussion This method is thread-safe. However, it is not guaranteed to
  75. * return on the main thread.
  76. */
  77. - (void)unsubscribeWithToken:(NSString *)token
  78. topic:(NSString *)topic
  79. options:(nullable NSDictionary *)options
  80. handler:(FIRMessagingTopicOperationCompletion)handler;
  81. /**
  82. * Asynchronously subscribe to the topic. Adds to the pending list of topic operations.
  83. * Retry in case of failures. This makes a repeated attempt to subscribe to the topic
  84. * as compared to the `subscribe` method above which tries once.
  85. *
  86. * @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
  87. * @param handler The handler that is invoked once the unsubscribe call ends.
  88. * In case of success, nil error is returned. Otherwise, an
  89. * appropriate error object is returned.
  90. */
  91. - (void)subscribeToTopic:(NSString *)topic
  92. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  93. /**
  94. * Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
  95. * Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
  96. * as compared to the `unsubscribe` method above which tries once.
  97. *
  98. * @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
  99. * @param handler The handler that is invoked once the unsubscribe call ends.
  100. * In case of success, nil error is returned. Otherwise, an
  101. * appropriate error object is returned.
  102. */
  103. - (void)unsubscribeFromTopic:(NSString *)topic
  104. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  105. /**
  106. * Schedule subscriptions sync.
  107. *
  108. * @param immediately YES if the sync should be scheduled immediately else NO if we can delay
  109. * the sync.
  110. */
  111. - (void)scheduleSync:(BOOL)immediately;
  112. /**
  113. * Adds the "/topics/" prefix to the topic.
  114. *
  115. * @param topic The topic to add the prefix to.
  116. *
  117. * @return The new topic name with the "/topics/" prefix added.
  118. */
  119. + (NSString *)addPrefixToTopic:(NSString *)topic;
  120. /**
  121. * Removes the "/topics/" prefix from the topic.
  122. *
  123. * @param topic The topic to remove the prefix from.
  124. *
  125. * @return The new topic name with the "/topics/" prefix removed.
  126. */
  127. + (NSString *)removePrefixFromTopic:(NSString *)topic;
  128. /**
  129. * Check if the topic name has "/topics/" prefix.
  130. *
  131. * @param topic The topic name to verify.
  132. *
  133. * @return YES if the topic name has "/topics/" prefix else NO.
  134. */
  135. + (BOOL)hasTopicsPrefix:(NSString *)topic;
  136. /**
  137. * Check if it's a valid topic name. This includes "/topics/" prefix in the topic name.
  138. *
  139. * @param topic The topic name to verify.
  140. *
  141. * @return YES if the topic name satisfies the regex "/topics/[a-zA-Z0-9-_.~%]{1,900}".
  142. */
  143. + (BOOL)isValidTopicWithPrefix:(NSString *)topic;
  144. @end
  145. NS_ASSUME_NONNULL_END