FIRMessagingPubSub.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "FIRMessagingTopicsCommon.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 handler The callback handler invoked when the subscribe call
  57. * ends. In case of success, a nil error is returned. Otherwise,
  58. * an appropriate error object is returned.
  59. * @discussion This method is thread-safe. However, it is not guaranteed to
  60. * return on the main thread.
  61. */
  62. - (void)subscribeWithToken:(NSString *)token
  63. topic:(NSString *)topic
  64. options:(NSDictionary *)options
  65. handler:(FIRMessagingTopicOperationCompletion)handler;
  66. /**
  67. * Unsubscribes an app instance from a topic, stopping it from receiving
  68. * any further messages sent to that topic.
  69. *
  70. * This is an asynchronous call. If the attempt to unsubscribe fails,
  71. * we invoke the `completion` callback passed in with an appropriate error.
  72. *
  73. * @param token The token used to subscribe to this topic.
  74. * @param topic The topic to unsubscribe from. Should be of the form
  75. * `"/topics/<topic-name>"`.
  76. * @param handler The handler that is invoked once the unsubscribe call ends.
  77. * In case of success, nil error is returned. Otherwise, an
  78. * appropriate error object is returned.
  79. * @discussion This method is thread-safe. However, it is not guaranteed to
  80. * return on the main thread.
  81. */
  82. - (void)unsubscribeWithToken:(NSString *)token
  83. topic:(NSString *)topic
  84. options:(NSDictionary *)options
  85. handler:(FIRMessagingTopicOperationCompletion)handler;
  86. /**
  87. * Asynchronously subscribe to the topic. Adds to the pending list of topic operations.
  88. * Retry in case of failures. This makes a repeated attempt to subscribe to the topic
  89. * as compared to the `subscribe` method above which tries once.
  90. *
  91. * @param topic The topic name to subscribe to. Should be of the form `"/topics/<topic-name>"`.
  92. */
  93. - (void)subscribeToTopic:(NSString *)topic;
  94. /**
  95. * Asynchronously unsubscribe from the topic. Adds to the pending list of topic operations.
  96. * Retry in case of failures. This makes a repeated attempt to unsubscribe from the topic
  97. * as compared to the `unsubscribe` method above which tries once.
  98. *
  99. * @param topic The topic name to unsubscribe from. Should be of the form `"/topics/<topic-name>"`.
  100. */
  101. - (void)unsubscribeFromTopic:(NSString *)topic;
  102. /**
  103. * Schedule subscriptions sync.
  104. *
  105. * @param immediately YES if the sync should be scheduled immediately else NO if we can delay
  106. * the sync.
  107. */
  108. - (void)scheduleSync:(BOOL)immediately;
  109. /**
  110. * Adds the "/topics/" prefix to the topic.
  111. *
  112. * @param topic The topic to add the prefix to.
  113. *
  114. * @return The new topic name with the "/topics/" prefix added.
  115. */
  116. + (NSString *)addPrefixToTopic:(NSString *)topic;
  117. /**
  118. * Check if the topic name has "/topics/" prefix.
  119. *
  120. * @param topic The topic name to verify.
  121. *
  122. * @return YES if the topic name has "/topics/" prefix else NO.
  123. */
  124. + (BOOL)hasTopicsPrefix:(NSString *)topic;
  125. /**
  126. * Check if it's a valid topic name. This includes "/topics/" prefix in the topic name.
  127. *
  128. * @param topic The topic name to verify.
  129. *
  130. * @return YES if the topic name satisfies the regex "/topics/[a-zA-Z0-9-_.~%]{1,900}".
  131. */
  132. + (BOOL)isValidTopicWithPrefix:(NSString *)topic;
  133. @end