FIRMessagingPubSub.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "FIRMessaging.h"
  17. @class FIRMessagingClient;
  18. @class FIRMessagingPubSubCache;
  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. @property(nonatomic, readonly, strong) FIRMessagingPubSubCache *cache;
  36. @property(nonatomic, readonly, strong) FIRMessagingClient *client;
  37. /**
  38. * Initializes an instance of FIRMessagingPubSub.
  39. *
  40. * @return An instance of FIRMessagingPubSub.
  41. */
  42. - (instancetype)initWithClient:(FIRMessagingClient *)client NS_DESIGNATED_INITIALIZER;
  43. /**
  44. * Subscribes an app instance to a topic, enabling it to receive messages
  45. * sent to that topic.
  46. *
  47. * This is an asynchronous call. If subscription fails, FIRMessaging
  48. * invokes the completion callback with the appropriate error.
  49. *
  50. * @see FIRMessagingPubSub unsubscribeWithToken:topic:handler:
  51. *
  52. * @param token The registration token as received from the InstanceID
  53. * library for a given `authorizedEntity` and "gcm" scope.
  54. * @param topic The topic to subscribe to. Should be of the form
  55. * `"/topics/<topic-name>"`.
  56. * @param options Unused parameter, please pass nil or empty dictionary.
  57. * @param handler The callback handler invoked when the subscribe call
  58. * ends. In case of success, a nil error is returned. Otherwise,
  59. * an appropriate error object is returned.
  60. * @discussion This method is thread-safe. However, it is not guaranteed to
  61. * return on the main thread.
  62. */
  63. - (void)subscribeWithToken:(NSString *)token
  64. topic:(NSString *)topic
  65. options:(NSDictionary *)options
  66. handler:(FIRMessagingTopicOperationCompletion)handler;
  67. /**
  68. * Unsubscribes an app instance from a topic, stopping it from receiving
  69. * any further messages sent to that topic.
  70. *
  71. * This is an asynchronous call. If the attempt to unsubscribe fails,
  72. * we invoke the `completion` callback passed in with an appropriate error.
  73. *
  74. * @param token The token used to subscribe to this topic.
  75. * @param topic The topic to unsubscribe from. Should be of the form
  76. * `"/topics/<topic-name>"`.
  77. * @param options Unused parameter, please pass nil or empty dictionary.
  78. * @param handler The handler that is invoked once the unsubscribe call ends.
  79. * In case of success, nil error is returned. Otherwise, an
  80. * appropriate error object is returned.
  81. * @discussion This method is thread-safe. However, it is not guaranteed to
  82. * return on the main thread.
  83. */
  84. - (void)unsubscribeWithToken:(NSString *)token
  85. topic:(NSString *)topic
  86. options:(NSDictionary *)options
  87. handler:(FIRMessagingTopicOperationCompletion)handler;
  88. /**
  89. * Asynchronously subscribe to the topic. Adds to the pending list of topic operations.
  90. * Retry in case of failures. This makes a repeated attempt to subscribe to the topic
  91. * as compared to the `subscribe` method above which tries once.
  92. *
  93. * @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
  94. * @param handler The handler that is invoked once the unsubscribe call ends.
  95. * In case of success, nil error is returned. Otherwise, an
  96. * appropriate error object is returned.
  97. */
  98. - (void)subscribeToTopic:(NSString *)topic
  99. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  100. /**
  101. * Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
  102. * Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
  103. * as compared to the `unsubscribe` method above which tries once.
  104. *
  105. * @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
  106. * @param handler The handler that is invoked once the unsubscribe call ends.
  107. * In case of success, nil error is returned. Otherwise, an
  108. * appropriate error object is returned.
  109. */
  110. - (void)unsubscribeFromTopic:(NSString *)topic
  111. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  112. /**
  113. * Schedule subscriptions sync.
  114. *
  115. * @param immediately YES if the sync should be scheduled immediately else NO if we can delay
  116. * the sync.
  117. */
  118. - (void)scheduleSync:(BOOL)immediately;
  119. /**
  120. * Adds the "/topics/" prefix to the topic.
  121. *
  122. * @param topic The topic to add the prefix to.
  123. *
  124. * @return The new topic name with the "/topics/" prefix added.
  125. */
  126. + (NSString *)addPrefixToTopic:(NSString *)topic;
  127. /**
  128. * Check if the topic name has "/topics/" prefix.
  129. *
  130. * @param topic The topic name to verify.
  131. *
  132. * @return YES if the topic name has "/topics/" prefix else NO.
  133. */
  134. + (BOOL)hasTopicsPrefix:(NSString *)topic;
  135. /**
  136. * Check if it's a valid topic name. This includes "/topics/" prefix in the topic name.
  137. *
  138. * @param topic The topic name to verify.
  139. *
  140. * @return YES if the topic name satisfies the regex "/topics/[a-zA-Z0-9-_.~%]{1,900}".
  141. */
  142. + (BOOL)isValidTopicWithPrefix:(NSString *)topic;
  143. @end