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.

156 lines
5.3 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. @class FIRReachabilityChecker;
  18. @class GPBMessage;
  19. @class FIRMessagingConnection;
  20. @class FIRMessagingDataMessageManager;
  21. @class FIRMessagingRmqManager;
  22. /**
  23. * Callback to handle MCS connection requests.
  24. *
  25. * @param error The error object if any while trying to connect with MCS else nil.
  26. */
  27. typedef void(^FIRMessagingConnectCompletionHandler)(NSError *error);
  28. @protocol FIRMessagingClientDelegate <NSObject>
  29. @end
  30. /**
  31. * The client handles the subscribe/unsubscribe for an unregistered senderID
  32. * and device. It also manages the FIRMessaging data connection, the exponential backoff
  33. * algorithm in case of registration failures, sign in failures and unregister
  34. * failures. It also handles the reconnect logic if the FIRMessaging connection is
  35. * broken off by some error during an active session.
  36. */
  37. @interface FIRMessagingClient : NSObject
  38. @property(nonatomic, readonly, strong) FIRMessagingConnection *connection;
  39. @property(nonatomic, readwrite, weak) FIRMessagingDataMessageManager *dataMessageManager;
  40. // Designated initializer
  41. - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
  42. reachability:(FIRReachabilityChecker *)reachability
  43. rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager;
  44. - (void)teardown;
  45. - (void)cancelAllRequests;
  46. #pragma mark - FIRMessaging subscribe
  47. /**
  48. * Update the subscription associated with the given token and topic.
  49. *
  50. * For a to-be-created subscription we check if the client is already
  51. * subscribed to the topic or not. If subscribed we should have the
  52. * subscriptionID in the cache and we return from there itself, else we call
  53. * the FIRMessaging backend to create a new subscription for the topic for this client.
  54. *
  55. * For delete subscription requests we delete the stored subscription in the
  56. * client and then invoke the FIRMessaging backend to delete the existing subscription
  57. * completely.
  58. *
  59. * @param token The token associated with the device.
  60. * @param topic The topic for which the subscription should be updated.
  61. * @param options The options to be passed in to the subscription request.
  62. * @param shouldDelete If YES this would delete the subscription from the cache
  63. * and also let the FIRMessaging backend know that we need to delete
  64. * the subscriptionID associated with this topic.
  65. * If NO we try to create a new subscription for the given
  66. * token and topic.
  67. * @param handler The handler to invoke once the subscription request
  68. * finishes.
  69. */
  70. - (void)updateSubscriptionWithToken:(NSString *)token
  71. topic:(NSString *)topic
  72. options:(NSDictionary *)options
  73. shouldDelete:(BOOL)shouldDelete
  74. handler:(FIRMessagingTopicOperationCompletion)handler;
  75. #pragma mark - MCS Connection
  76. /**
  77. * Create a MCS connection.
  78. *
  79. * @param handler The handler to be invokend once the connection is setup. If
  80. * setting up the connection fails we invoke the handler with
  81. * an appropriate error object.
  82. */
  83. - (void)connectWithHandler:(FIRMessagingConnectCompletionHandler)handler;
  84. /**
  85. * Disconnect the current MCS connection. If there is no valid connection this
  86. * should be a NO-OP.
  87. */
  88. - (void)disconnect;
  89. #pragma mark - MCS Connection State
  90. /**
  91. * If we are connected to MCS or not. This doesn't take into account the fact if
  92. * the client has been signed in(verified) by MCS.
  93. *
  94. * @return YES if we are signed in or connecting and trying to sign-in else NO.
  95. */
  96. @property(nonatomic, readonly) BOOL isConnected;
  97. /**
  98. * If we have an active MCS connection
  99. *
  100. * @return YES if we have an active MCS connection else NO.
  101. */
  102. @property(nonatomic, readonly) BOOL isConnectionActive;
  103. /**
  104. * If we should be connected to MCS
  105. *
  106. * @return YES if we have attempted a connection and not requested to disconect.
  107. */
  108. @property(nonatomic, readonly) BOOL shouldStayConnected;
  109. /**
  110. * Schedule a retry to connect to MCS. If `immediately` is `YES` try to
  111. * schedule a retry now else retry with some delay.
  112. *
  113. * @param immediately Should retry right now.
  114. */
  115. - (void)retryConnectionImmediately:(BOOL)immediately;
  116. #pragma mark - Messages
  117. /**
  118. * Send a message over the MCS connection.
  119. *
  120. * @param message Message to be sent.
  121. */
  122. - (void)sendMessage:(GPBMessage *)message;
  123. /**
  124. * Send message if we have an active MCS connection. If not cache the message
  125. * for this session and in case we are able to re-establish the connection try
  126. * again else drop it. This should only be used for TTL=0 messages for now.
  127. *
  128. * @param message Message to be sent.
  129. */
  130. - (void)sendOnConnectOrDrop:(GPBMessage *)message;
  131. @end