FIRMessaging.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. __IOS_AVAILABLE(10.0)
  85. @protocol FIRMessagingDelegate <NSObject>
  86. /// The callback to handle data message received via FCM for devices running iOS 10 or above.
  87. - (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage;
  88. @end
  89. /**
  90. * Firebase Messaging lets you reliably deliver messages at no cost.
  91. *
  92. * To send or receive messages, the app must get a
  93. * registration token from FIRInstanceID. This token authorizes an
  94. * app server to send messages to an app instance.
  95. *
  96. * In order to receive FIRMessaging messages, declare `application:didReceiveRemoteNotification:`.
  97. *
  98. *
  99. */
  100. @interface FIRMessaging : NSObject
  101. /**
  102. * Delegate to handle remote data messages received via FCM for devices running iOS 10 or above.
  103. */
  104. @property(nonatomic, weak, nullable) id<FIRMessagingDelegate> remoteMessageDelegate;
  105. /**
  106. * FIRMessaging
  107. *
  108. * @return An instance of FIRMessaging.
  109. */
  110. + (nonnull instancetype)messaging NS_SWIFT_NAME(messaging());
  111. /**
  112. * Unavailable. Use +messaging instead.
  113. */
  114. - (nonnull instancetype)init __attribute__((unavailable("Use +messaging instead.")));
  115. #pragma mark - Connect
  116. /**
  117. * Create a FIRMessaging data connection which will be used to send the data notifications
  118. * sent by your server. It will also be used to send ACKS and other messages based
  119. * on the FIRMessaging ACKS and other messages based on the FIRMessaging protocol.
  120. *
  121. *
  122. * @param handler The handler to be invoked once the connection is established.
  123. * If the connection fails we invoke the handler with an
  124. * appropriate error code letting you know why it failed. At
  125. * the same time, FIRMessaging performs exponential backoff to retry
  126. * establishing a connection and invoke the handler when successful.
  127. */
  128. - (void)connectWithCompletion:(nonnull FIRMessagingConnectCompletion)handler;
  129. /**
  130. * Disconnect the current FIRMessaging data connection. This stops any attempts to
  131. * connect to FIRMessaging. Calling this on an already disconnected client is a no-op.
  132. *
  133. * Call this before `teardown` when your app is going to the background.
  134. * Since the FIRMessaging connection won't be allowed to live when in background it is
  135. * prudent to close the connection.
  136. */
  137. - (void)disconnect;
  138. #pragma mark - Topics
  139. /**
  140. * Asynchronously subscribes to a topic.
  141. *
  142. * @param topic The name of the topic, for example, @"sports".
  143. */
  144. - (void)subscribeToTopic:(nonnull NSString *)topic;
  145. /**
  146. * Asynchronously unsubscribe from a topic.
  147. *
  148. * @param topic The name of the topic, for example @"sports".
  149. */
  150. - (void)unsubscribeFromTopic:(nonnull NSString *)topic;
  151. #pragma mark - Upstream
  152. /**
  153. * Sends an upstream ("device to cloud") message.
  154. *
  155. * The message is queued if we don't have an active connection.
  156. * You can only use the upstream feature if your FCM implementation
  157. * uses the XMPP server protocol.
  158. *
  159. * @param message Key/Value pairs to be sent. Values must be String, any
  160. * other type will be ignored.
  161. * @param to A string identifying the receiver of the message. For FCM
  162. * project IDs the value is `SENDER_ID@gcm.googleapis.com`.
  163. * @param messageID The ID of the message. This is generated by the application. It
  164. * must be unique for each message generated by this application.
  165. * It allows error callbacks and debugging, to uniquely identify
  166. * each message.
  167. * @param ttl The time to live for the message. In case we aren't able to
  168. * send the message before the TTL expires we will send you a
  169. * callback. If 0, we'll attempt to send immediately and return
  170. * an error if we're not connected. Otherwise, the message will
  171. * be queued. As for server-side messages, we don't return an error
  172. * if the message has been dropped because of TTL; this can happen
  173. * on the server side, and it would require extra communication.
  174. */
  175. - (void)sendMessage:(nonnull NSDictionary *)message
  176. to:(nonnull NSString *)receiver
  177. withMessageID:(nonnull NSString *)messageID
  178. timeToLive:(int64_t)ttl;
  179. #pragma mark - Analytics
  180. /**
  181. * Use this to track message delivery and analytics for messages, typically
  182. * when you receive a notification in `application:didReceiveRemoteNotification:`.
  183. * However, you only need to call this if you set the `FirebaseAppDelegateProxyEnabled`
  184. * flag to NO in your Info.plist. If `FirebaseAppDelegateProxyEnabled` is either missing
  185. * or set to YES in your Info.plist, the library will call this automatically.
  186. *
  187. * @param message The downstream message received by the application.
  188. *
  189. * @return Information about the downstream message.
  190. */
  191. - (nonnull FIRMessagingMessageInfo *)appDidReceiveMessage:(nonnull NSDictionary *)message;
  192. @end