You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.4 KiB

6 years ago
  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. NS_ASSUME_NONNULL_BEGIN
  18. @class FIRMessagingClient;
  19. @class FIRMessagingPubSubCache;
  20. /**
  21. * FIRMessagingPubSub provides a publish-subscribe model for sending FIRMessaging topic messages.
  22. *
  23. * An app can subscribe to different topics defined by the
  24. * developer. The app server can then send messages to the subscribed devices
  25. * without having to maintain topic-subscribers mapping. Topics do not
  26. * need to be explicitly created before subscribing or publishing—they
  27. * are automatically created when publishing or subscribing.
  28. *
  29. * Messages published to the topic will be received as regular FIRMessaging messages
  30. * with `"from"` set to `"/topics/myTopic"`.
  31. *
  32. * Only topic names that match the pattern `"/topics/[a-zA-Z0-9-_.~%]{1,900}"`
  33. * are allowed for subscribing and publishing.
  34. */
  35. @interface FIRMessagingPubSub : NSObject
  36. @property(nonatomic, readonly, strong) FIRMessagingPubSubCache *cache;
  37. @property(nonatomic, readonly, strong) FIRMessagingClient *client;
  38. /**
  39. * Initializes an instance of FIRMessagingPubSub.
  40. *
  41. * @return An instance of FIRMessagingPubSub.
  42. */
  43. - (instancetype)initWithClient:(FIRMessagingClient *)client NS_DESIGNATED_INITIALIZER;
  44. /**
  45. * Subscribes an app instance to a topic, enabling it to receive messages
  46. * sent to that topic.
  47. *
  48. * This is an asynchronous call. If subscription fails, FIRMessaging
  49. * invokes the completion callback with the appropriate error.
  50. *
  51. * @see FIRMessagingPubSub unsubscribeWithToken:topic:handler:
  52. *
  53. * @param token The registration token as received from the InstanceID
  54. * library for a given `authorizedEntity` and "gcm" scope.
  55. * @param topic The topic to subscribe to. Should be of the form
  56. * `"/topics/<topic-name>"`.
  57. * @param options Unused parameter, please pass nil or empty dictionary.
  58. * @param handler The callback handler invoked when the subscribe call
  59. * ends. In case of success, a nil error is returned. Otherwise,
  60. * an appropriate error object is returned.
  61. * @discussion This method is thread-safe. However, it is not guaranteed to
  62. * return on the main thread.
  63. */
  64. - (void)subscribeWithToken:(NSString *)token
  65. topic:(NSString *)topic
  66. options:(nullable NSDictionary *)options
  67. handler:(FIRMessagingTopicOperationCompletion)handler;
  68. /**
  69. * Unsubscribes an app instance from a topic, stopping it from receiving
  70. * any further messages sent to that topic.
  71. *
  72. * This is an asynchronous call. If the attempt to unsubscribe fails,
  73. * we invoke the `completion` callback passed in with an appropriate error.
  74. *
  75. * @param token The token used to subscribe to this topic.
  76. * @param topic The topic to unsubscribe from. Should be of the form
  77. * `"/topics/<topic-name>"`.
  78. * @param options Unused parameter, please pass nil or empty dictionary.
  79. * @param handler The handler that is invoked once the unsubscribe call ends.
  80. * In case of success, nil error is returned. Otherwise, an
  81. * appropriate error object is returned.
  82. * @discussion This method is thread-safe. However, it is not guaranteed to
  83. * return on the main thread.
  84. */
  85. - (void)unsubscribeWithToken:(NSString *)token
  86. topic:(NSString *)topic
  87. options:(nullable NSDictionary *)options
  88. handler:(FIRMessagingTopicOperationCompletion)handler;
  89. /**
  90. * Asynchronously subscribe to the topic. Adds to the pending list of topic operations.
  91. * Retry in case of failures. This makes a repeated attempt to subscribe to the topic
  92. * as compared to the `subscribe` method above which tries once.
  93. *
  94. * @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
  95. * @param handler The handler that is invoked once the unsubscribe call ends.
  96. * In case of success, nil error is returned. Otherwise, an
  97. * appropriate error object is returned.
  98. */
  99. - (void)subscribeToTopic:(NSString *)topic
  100. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  101. /**
  102. * Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
  103. * Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
  104. * as compared to the `unsubscribe` method above which tries once.
  105. *
  106. * @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
  107. * @param handler The handler that is invoked once the unsubscribe call ends.
  108. * In case of success, nil error is returned. Otherwise, an
  109. * appropriate error object is returned.
  110. */
  111. - (void)unsubscribeFromTopic:(NSString *)topic
  112. handler:(nullable FIRMessagingTopicOperationCompletion)handler;
  113. /**
  114. * Schedule subscriptions sync.
  115. *
  116. * @param immediately YES if the sync should be scheduled immediately else NO if we can delay
  117. * the sync.
  118. */
  119. - (void)scheduleSync:(BOOL)immediately;
  120. /**
  121. * Adds the "/topics/" prefix to the topic.
  122. *
  123. * @param topic The topic to add the prefix to.
  124. *
  125. * @return The new topic name with the "/topics/" prefix added.
  126. */
  127. + (NSString *)addPrefixToTopic:(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