FIRMessaging.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #import <Foundation/Foundation.h>
  2. /**
  3. * The completion handler invoked once the data connection with FIRMessaging is
  4. * established. The data connection is used to send a continous stream of
  5. * data and all the FIRMessaging data notifications arrive through this connection.
  6. * Once the connection is established we invoke the callback with `nil` error.
  7. * Correspondingly if we get an error while trying to establish a connection
  8. * we invoke the handler with an appropriate error object and do an
  9. * exponential backoff to try and connect again unless successful.
  10. *
  11. * @param error The error object if any describing why the data connection
  12. * to FIRMessaging failed.
  13. */
  14. typedef void(^FIRMessagingConnectCompletion)(NSError * __nullable error);
  15. /**
  16. * Notification sent when the upstream message has been delivered
  17. * successfully to the server. The notification object will be the messageID
  18. * of the successfully delivered message.
  19. */
  20. FOUNDATION_EXPORT NSString * __nonnull const FIRMessagingSendSuccessNotification;
  21. /**
  22. * Notification sent when the upstream message was failed to be sent to the
  23. * server. The notification object will be the messageID of the failed
  24. * message. The userInfo dictionary will contain the relevant error
  25. * information for the failure.
  26. */
  27. FOUNDATION_EXPORT NSString * __nonnull const FIRMessagingSendErrorNotification;
  28. /**
  29. * Notification sent when the Firebase messaging server deletes pending
  30. * messages due to exceeded storage limits. This may occur, for example, when
  31. * the device cannot be reached for an extended period of time.
  32. *
  33. * It is recommended to retrieve any missing messages directly from the
  34. * server.
  35. */
  36. FOUNDATION_EXPORT NSString * __nonnull const FIRMessagingMessagesDeletedNotification;
  37. /**
  38. * @enum FIRMessagingError
  39. */
  40. typedef NS_ENUM(NSUInteger, FIRMessagingError) {
  41. /// Unknown error.
  42. FIRMessagingErrorUnknown = 0,
  43. /// FIRMessaging couldn't validate request from this client.
  44. FIRMessagingErrorAuthentication = 1,
  45. /// InstanceID service cannot be accessed.
  46. FIRMessagingErrorNoAccess = 2,
  47. /// Request to InstanceID backend timed out.
  48. FIRMessagingErrorTimeout = 3,
  49. /// No network available to reach the servers.
  50. FIRMessagingErrorNetwork = 4,
  51. /// Another similar operation in progress, bailing this one.
  52. FIRMessagingErrorOperationInProgress = 5,
  53. /// Some parameters of the request were invalid.
  54. FIRMessagingErrorInvalidRequest = 7,
  55. };
  56. /// Status for the downstream message received by the app.
  57. typedef NS_ENUM(NSInteger, FIRMessagingMessageStatus) {
  58. /// Unknown status.
  59. FIRMessagingMessageStatusUnknown,
  60. /// New downstream message received by the app.
  61. FIRMessagingMessageStatusNew,
  62. };
  63. /// Information about a downstream message received by the app.
  64. @interface FIRMessagingMessageInfo : NSObject
  65. /// The status of the downstream message
  66. @property(nonatomic, readonly, assign) FIRMessagingMessageStatus status;
  67. @end
  68. /**
  69. * A remote data message received by the app via FCM (not just the APNs interface).
  70. *
  71. * This is only for devices running iOS 10 or above. To support devices running iOS 9 or below, use
  72. * the local and remote notifications handlers defined in UIApplicationDelegate protocol.
  73. */
  74. @interface FIRMessagingRemoteMessage : NSObject
  75. /// The downstream message received by the application.
  76. @property(nonatomic, readonly, strong, nonnull) NSDictionary *appData;
  77. @end
  78. /**
  79. * A protocol to receive data message via FCM for devices running iOS 10 or above.
  80. *
  81. * To support devices running iOS 9 or below, use the local and remote notifications handlers
  82. * defined in UIApplicationDelegate protocol.
  83. */
  84. @protocol FIRMessagingDelegate <NSObject>
  85. /// The callback to handle data message received via FCM for devices running iOS 10 or above.
  86. - (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage;
  87. @end
  88. /**
  89. * Firebase Messaging lets you reliably deliver messages at no cost.
  90. *
  91. * To send or receive messages, the app must get a
  92. * registration token from FIRInstanceID. This token authorizes an
  93. * app server to send messages to an app instance.
  94. *
  95. * In order to receive FIRMessaging messages, declare `application:didReceiveRemoteNotification:`.
  96. *
  97. *
  98. */
  99. @interface FIRMessaging : NSObject
  100. /**
  101. * Delegate to handle remote data messages received via FCM for devices running iOS 10 or above.
  102. */
  103. @property(nonatomic, weak, nullable) id<FIRMessagingDelegate> remoteMessageDelegate;
  104. /**
  105. * FIRMessaging
  106. *
  107. * @return An instance of FIRMessaging.
  108. */
  109. + (nonnull instancetype)messaging NS_SWIFT_NAME(messaging());
  110. /**
  111. * Unavailable. Use +messaging instead.
  112. */
  113. - (nonnull instancetype)init __attribute__((unavailable("Use +messaging instead.")));
  114. #pragma mark - Connect
  115. /**
  116. * Create a FIRMessaging data connection which will be used to send the data notifications
  117. * sent by your server. It will also be used to send ACKS and other messages based
  118. * on the FIRMessaging ACKS and other messages based on the FIRMessaging protocol.
  119. *
  120. *
  121. * @param handler The handler to be invoked once the connection is established.
  122. * If the connection fails we invoke the handler with an
  123. * appropriate error code letting you know why it failed. At
  124. * the same time, FIRMessaging performs exponential backoff to retry
  125. * establishing a connection and invoke the handler when successful.
  126. */
  127. - (void)connectWithCompletion:(nonnull FIRMessagingConnectCompletion)handler;
  128. /**
  129. * Disconnect the current FIRMessaging data connection. This stops any attempts to
  130. * connect to FIRMessaging. Calling this on an already disconnected client is a no-op.
  131. *
  132. * Call this before `teardown` when your app is going to the background.
  133. * Since the FIRMessaging connection won't be allowed to live when in background it is
  134. * prudent to close the connection.
  135. */
  136. - (void)disconnect;
  137. #pragma mark - Topics
  138. /**
  139. * Asynchronously subscribes to a topic.
  140. *
  141. * @param topic The name of the topic, for example, @"sports".
  142. */
  143. - (void)subscribeToTopic:(nonnull NSString *)topic;
  144. /**
  145. * Asynchronously unsubscribe from a topic.
  146. *
  147. * @param topic The name of the topic, for example @"sports".
  148. */
  149. - (void)unsubscribeFromTopic:(nonnull NSString *)topic;
  150. #pragma mark - Upstream
  151. /**
  152. * Sends an upstream ("device to cloud") message.
  153. *
  154. * The message is queued if we don't have an active connection.
  155. * You can only use the upstream feature if your FCM implementation
  156. * uses the XMPP server protocol.
  157. *
  158. * @param message Key/Value pairs to be sent. Values must be String, any
  159. * other type will be ignored.
  160. * @param to A string identifying the receiver of the message. For FCM
  161. * project IDs the value is `SENDER_ID@gcm.googleapis.com`.
  162. * @param messageID The ID of the message. This is generated by the application. It
  163. * must be unique for each message generated by this application.
  164. * It allows error callbacks and debugging, to uniquely identify
  165. * each message.
  166. * @param ttl The time to live for the message. In case we aren't able to
  167. * send the message before the TTL expires we will send you a
  168. * callback. If 0, we'll attempt to send immediately and return
  169. * an error if we're not connected. Otherwise, the message will
  170. * be queued. As for server-side messages, we don't return an error
  171. * if the message has been dropped because of TTL; this can happen
  172. * on the server side, and it would require extra communication.
  173. */
  174. - (void)sendMessage:(nonnull NSDictionary *)message
  175. to:(nonnull NSString *)receiver
  176. withMessageID:(nonnull NSString *)messageID
  177. timeToLive:(int64_t)ttl;
  178. #pragma mark - Analytics
  179. /**
  180. * Use this to track message delivery and analytics for messages, typically
  181. * when you receive a notification in `application:didReceiveRemoteNotification:`.
  182. * However, you only need to call this if you set the `FirebaseAppDelegateProxyEnabled`
  183. * flag to NO in your Info.plist. If `FirebaseAppDelegateProxyEnabled` is either missing
  184. * or set to YES in your Info.plist, the library will call this automatically.
  185. *
  186. * @param message The downstream message received by the application.
  187. *
  188. * @return Information about the downstream message.
  189. */
  190. - (nonnull FIRMessagingMessageInfo *)appDidReceiveMessage:(nonnull NSDictionary *)message;
  191. @end