FIRMessaging.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. /**
  19. * @related FIRMessaging
  20. *
  21. * The completion handler invoked when the registration token returns.
  22. * If the call fails we return the appropriate `error code`, described by
  23. * `FIRMessagingError`.
  24. *
  25. * @param FCMToken The valid registration token returned by FCM.
  26. * @param error The error describing why a token request failed. The error code
  27. * will match a value from the FIRMessagingError enumeration.
  28. */
  29. typedef void (^FIRMessagingFCMTokenFetchCompletion)(NSString *_Nullable FCMToken,
  30. NSError *_Nullable error)
  31. NS_SWIFT_NAME(MessagingFCMTokenFetchCompletion);
  32. /**
  33. * @related FIRMessaging
  34. *
  35. * The completion handler invoked when the registration token deletion request is
  36. * completed. If the call fails we return the appropriate `error code`, described
  37. * by `FIRMessagingError`.
  38. *
  39. * @param error The error describing why a token deletion failed. The error code
  40. * will match a value from the FIRMessagingError enumeration.
  41. */
  42. typedef void (^FIRMessagingDeleteFCMTokenCompletion)(NSError *_Nullable error)
  43. NS_SWIFT_NAME(MessagingDeleteFCMTokenCompletion);
  44. /**
  45. * Callback to invoke once the HTTP call to FIRMessaging backend for updating
  46. * subscription finishes.
  47. *
  48. * @param error The error which occurred while updating the subscription topic
  49. * on the FIRMessaging server. This will be nil in case the operation
  50. * was successful, or if the operation was cancelled.
  51. */
  52. typedef void (^FIRMessagingTopicOperationCompletion)(NSError *_Nullable error);
  53. /**
  54. * Notification sent when the FCM registration token has been refreshed. Please use the
  55. * FIRMessaging delegate method `messaging:didReceiveRegistrationToken:` to receive current and
  56. * updated tokens.
  57. */
  58. // clang-format off
  59. // clang-format12 merges the next two lines.
  60. FOUNDATION_EXPORT const NSNotificationName FIRMessagingRegistrationTokenRefreshedNotification
  61. NS_SWIFT_NAME(MessagingRegistrationTokenRefreshed);
  62. // clang-format on
  63. /**
  64. * @enum FIRMessagingError
  65. */
  66. typedef NS_ENUM(NSUInteger, FIRMessagingError) {
  67. /// Unknown error.
  68. FIRMessagingErrorUnknown = 0,
  69. /// FIRMessaging couldn't validate request from this client.
  70. FIRMessagingErrorAuthentication = 1,
  71. /// InstanceID service cannot be accessed.
  72. FIRMessagingErrorNoAccess = 2,
  73. /// Request to InstanceID backend timed out.
  74. FIRMessagingErrorTimeout = 3,
  75. /// No network available to reach the servers.
  76. FIRMessagingErrorNetwork = 4,
  77. /// Another similar operation in progress, bailing this one.
  78. FIRMessagingErrorOperationInProgress = 5,
  79. /// Some parameters of the request were invalid.
  80. FIRMessagingErrorInvalidRequest = 7,
  81. /// Topic name is invalid for subscription/unsubscription.
  82. FIRMessagingErrorInvalidTopicName = 8,
  83. } NS_SWIFT_NAME(MessagingError);
  84. /// Status for the downstream message received by the app.
  85. typedef NS_ENUM(NSInteger, FIRMessagingMessageStatus) {
  86. /// Unknown status.
  87. FIRMessagingMessageStatusUnknown,
  88. /// New downstream message received by the app.
  89. FIRMessagingMessageStatusNew,
  90. } NS_SWIFT_NAME(MessagingMessageStatus);
  91. /**
  92. * The APNs token type for the app. If the token type is set to `UNKNOWN`
  93. * Firebase Messaging will implicitly try to figure out what the actual token type
  94. * is from the provisioning profile.
  95. * Unless you really need to specify the type, you should use the `APNSToken`
  96. * property instead.
  97. */
  98. typedef NS_ENUM(NSInteger, FIRMessagingAPNSTokenType) {
  99. /// Unknown token type.
  100. FIRMessagingAPNSTokenTypeUnknown,
  101. /// Sandbox token type.
  102. FIRMessagingAPNSTokenTypeSandbox,
  103. /// Production token type.
  104. FIRMessagingAPNSTokenTypeProd,
  105. } NS_SWIFT_NAME(MessagingAPNSTokenType);
  106. /// Information about a downstream message received by the app.
  107. NS_SWIFT_NAME(MessagingMessageInfo)
  108. @interface FIRMessagingMessageInfo : NSObject
  109. /// The status of the downstream message
  110. @property(nonatomic, readonly, assign) FIRMessagingMessageStatus status;
  111. @end
  112. @class FIRMessaging;
  113. @class FIRMessagingExtensionHelper;
  114. /**
  115. * A protocol to handle token update or data message delivery from FCM.
  116. *
  117. */
  118. NS_SWIFT_NAME(MessagingDelegate)
  119. @protocol FIRMessagingDelegate <NSObject>
  120. @optional
  121. /// This method will be called once a token is available, or has been refreshed. Typically it
  122. /// will be called once per app start, but may be called more often, if token is invalidated or
  123. /// updated. In this method, you should perform operations such as:
  124. ///
  125. /// * Uploading the FCM token to your application server, so targeted notifications can be sent.
  126. ///
  127. /// * Subscribing to any topics.
  128. - (void)messaging:(FIRMessaging *)messaging
  129. didReceiveRegistrationToken:(nullable NSString *)fcmToken
  130. NS_SWIFT_NAME(messaging(_:didReceiveRegistrationToken:));
  131. @end
  132. /**
  133. * Firebase Messaging lets you reliably deliver messages at no cost.
  134. *
  135. * To send or receive messages, the app must get a
  136. * registration token. This token authorizes an
  137. * app server to send messages to an app instance.
  138. *
  139. * In order to receive FIRMessaging messages, declare
  140. * `application:didReceiveRemoteNotification::fetchCompletionHandler:`.
  141. */
  142. NS_SWIFT_NAME(Messaging)
  143. @interface FIRMessaging : NSObject
  144. /**
  145. * Delegate to handle FCM token refreshes, and remote data messages received via FCM direct channel.
  146. */
  147. @property(nonatomic, weak, nullable) id<FIRMessagingDelegate> delegate;
  148. /**
  149. * FIRMessaging
  150. *
  151. * @return An instance of FIRMessaging.
  152. */
  153. + (instancetype)messaging NS_SWIFT_NAME(messaging());
  154. /**
  155. * FIRMessagingExtensionHelper
  156. *
  157. * Use FIRMessagingExtensionHelper to populate rich UI contents for your notifications.
  158. * e.g. If an image URL is set in your notification payload or on the console, call
  159. * FIRMessagingExtensionHelper API to render it on your notification.
  160. *
  161. * @return An instance of FIRMessagingExtensionHelper that handles the extensions API.
  162. */
  163. + (FIRMessagingExtensionHelper *)extensionHelper NS_SWIFT_NAME(serviceExtension())
  164. NS_AVAILABLE(10.14, 10.0);
  165. /**
  166. * Unavailable. Use +messaging instead.
  167. */
  168. - (instancetype)init __attribute__((unavailable("Use +messaging instead.")));
  169. #pragma mark - APNs
  170. /**
  171. * This property is used to set the APNs Token received by the application delegate.
  172. *
  173. * FIRMessaging uses method swizzling to ensure that the APNs token is set
  174. * automatically. However, if you have disabled swizzling by setting
  175. * `FirebaseAppDelegateProxyEnabled` to `NO` in your app's
  176. * Info.plist, you should manually set the APNs token in your application
  177. * delegate's `-application:didRegisterForRemoteNotificationsWithDeviceToken:`
  178. * method.
  179. *
  180. * If you would like to set the type of the APNs token, rather than relying on
  181. * automatic detection, see: `-setAPNSToken:type:`.
  182. */
  183. @property(nonatomic, copy, nullable) NSData *APNSToken NS_SWIFT_NAME(apnsToken);
  184. /**
  185. * Set APNs token for the application. This APNs token will be used to register
  186. * with Firebase Messaging using `FCMToken` or
  187. * `tokenWithAuthorizedEntity:scope:options:handler`.
  188. *
  189. * @param apnsToken The APNs token for the application.
  190. * @param type The type of APNs token. Debug builds should use
  191. * FIRMessagingAPNSTokenTypeSandbox. Alternatively, you can supply
  192. * FIRMessagingAPNSTokenTypeUnknown to have the type automatically
  193. * detected based on your provisioning profile.
  194. */
  195. - (void)setAPNSToken:(NSData *)apnsToken type:(FIRMessagingAPNSTokenType)type;
  196. #pragma mark - FCM Tokens
  197. /**
  198. * Is Firebase Messaging token auto generation enabled? If this flag is disabled, Firebase
  199. * Messaging will not generate token automatically for message delivery.
  200. *
  201. * If this flag is disabled, Firebase Messaging does not generate new tokens automatically for
  202. * message delivery. If this flag is enabled, FCM generates a registration token on application
  203. * start when there is no existing valid token and periodically refreshes the token and sends
  204. * data to Firebase backend.
  205. *
  206. * This setting is persisted, and is applied on future invocations of your application. Once
  207. * explicitly set, it overrides any settings in your Info.plist.
  208. *
  209. * By default, FCM automatic initialization is enabled. If you need to change the
  210. * default (for example, because you want to prompt the user before getting token)
  211. * set FirebaseMessagingAutoInitEnabled to false in your application's Info.plist.
  212. */
  213. @property(nonatomic, assign, getter=isAutoInitEnabled) BOOL autoInitEnabled;
  214. /**
  215. * The FCM registration token is used to identify this device so that FCM can send notifications to
  216. * it. It is associated with your APNs token when the APNs token is supplied, so messages sent to
  217. * the FCM token will be delivered over APNs.
  218. *
  219. * The FCM registration token is sometimes refreshed automatically. In your FIRMessaging delegate,
  220. * the delegate method `messaging:didReceiveRegistrationToken:` will be called once a token is
  221. * available, or has been refreshed. Typically it should be called once per app start, but
  222. * may be called more often if the token is invalidated or updated.
  223. *
  224. * Once you have an FCM registration token, you should send it to your application server, so it can
  225. * use the FCM token to send notifications to your device.
  226. */
  227. @property(nonatomic, readonly, nullable) NSString *FCMToken NS_SWIFT_NAME(fcmToken);
  228. /**
  229. * Asynchronously gets the default FCM registration token.
  230. *
  231. * This creates a Firebase Installations ID, if one does not exist, and sends information about the
  232. * application and the device to the Firebase backend. A network connection is required for the
  233. * method to succeed. To stop this, see `Messaging.isAutoInitEnabled`,
  234. * `Messaging.delete(completion:)` and `Installations.delete(completion:)`.
  235. *
  236. * @param completion The completion handler to handle the token request.
  237. */
  238. - (void)tokenWithCompletion:(void (^)(NSString *__nullable token,
  239. NSError *__nullable error))completion;
  240. /**
  241. * Asynchronously deletes the default FCM registration token.
  242. *
  243. * This does not delete all tokens for non-default sender IDs, See `Messaging.delete(completion:)`
  244. * for deleting all of them. To prevent token auto generation, see `Messaging.isAutoInitEnabled`.
  245. *
  246. * @param completion The completion handler to handle the token deletion.
  247. */
  248. - (void)deleteTokenWithCompletion:(void (^)(NSError *__nullable error))completion;
  249. /**
  250. * Retrieves an FCM registration token for a particular Sender ID. This can be used to allow
  251. * multiple senders to send notifications to the same device. By providing a different Sender
  252. * ID than your default when fetching a token, you can create a new FCM token which you can
  253. * give to a different sender. Both tokens will deliver notifications to your device, and you
  254. * can revoke a token when you need to.
  255. *
  256. * This registration token is not cached by FIRMessaging. FIRMessaging should have an APNs
  257. * token set before calling this to ensure that notifications can be delivered via APNs using
  258. * this FCM token. You may re-retrieve the FCM token once you have the APNs token set, to
  259. * associate it with the FCM token. The default FCM token is automatically associated with
  260. * the APNs token, if the APNs token data is available.
  261. *
  262. * This creates a Firebase Installations ID, if one does not exist, and sends information
  263. * about the application and the device to the Firebase backend.
  264. *
  265. * @param senderID The Sender ID for a particular Firebase project.
  266. * @param completion The completion handler to handle the token request.
  267. */
  268. - (void)retrieveFCMTokenForSenderID:(NSString *)senderID
  269. completion:(void (^)(NSString *_Nullable FCMToken,
  270. NSError *_Nullable error))completion
  271. NS_SWIFT_NAME(retrieveFCMToken(forSenderID:completion:));
  272. /**
  273. * Invalidates an FCM token for a particular Sender ID. That Sender ID cannot no longer send
  274. * notifications to that FCM token. This does not delete the Firebase Installations ID that may have
  275. * been created when generating the token. See `Installations.delete(completion:)`.
  276. *
  277. * @param senderID The senderID for a particular Firebase project.
  278. * @param completion The completion handler to handle the token deletion.
  279. */
  280. - (void)deleteFCMTokenForSenderID:(NSString *)senderID
  281. completion:(void (^)(NSError *_Nullable error))completion
  282. NS_SWIFT_NAME(deleteFCMToken(forSenderID:completion:));
  283. #pragma mark - Topics
  284. /**
  285. * Asynchronously subscribes to a topic. This uses the default FCM registration token to identify
  286. * the app instance and periodically sends data to the Firebase backend. To stop this, see
  287. * `Messaging.delete(completion:)` and `Installations.delete(completion:)`.
  288. *
  289. * @param topic The name of the topic, for example, @"sports".
  290. */
  291. - (void)subscribeToTopic:(NSString *)topic NS_SWIFT_NAME(subscribe(toTopic:));
  292. /**
  293. * Asynchronously subscribe to the provided topic, retrying on failure. This uses the default FCM
  294. * registration token to identify the app instance and periodically sends data to the Firebase
  295. * backend. To stop this, see `Messaging.delete(completion:)` and
  296. * `Installations.delete(completion:)`.
  297. *
  298. * @param topic The topic name to subscribe to, for example, @"sports".
  299. * @param completion The completion that is invoked once the subscribe call ends.
  300. * In case of success, nil error is returned. Otherwise, an
  301. * appropriate error object is returned.
  302. */
  303. - (void)subscribeToTopic:(nonnull NSString *)topic
  304. completion:(void (^_Nullable)(NSError *_Nullable error))completion;
  305. /**
  306. * Asynchronously unsubscribe from a topic. This uses a FCM Token
  307. * to identify the app instance and periodically sends data to the Firebase backend. To stop this,
  308. * see `Messaging.delete(completion:)` and `Installations.delete(completion:)`.
  309. *
  310. * @param topic The name of the topic, for example @"sports".
  311. */
  312. - (void)unsubscribeFromTopic:(NSString *)topic NS_SWIFT_NAME(unsubscribe(fromTopic:));
  313. /**
  314. * Asynchronously unsubscribe from the provided topic, retrying on failure. This uses a FCM Token
  315. * to identify the app instance and periodically sends data to the Firebase backend. To stop this,
  316. * see `Messaging.delete(completion:)` and `Installations.delete(completion:)`.
  317. *
  318. * @param topic The topic name to unsubscribe from, for example @"sports".
  319. * @param completion The completion that is invoked once the unsubscribe call ends.
  320. * In case of success, nil error is returned. Otherwise, an
  321. * appropriate error object is returned.
  322. */
  323. - (void)unsubscribeFromTopic:(nonnull NSString *)topic
  324. completion:(void (^_Nullable)(NSError *_Nullable error))completion;
  325. #pragma mark - Analytics
  326. /**
  327. * Use this to track message delivery and analytics for messages, typically
  328. * when you receive a notification in `application:didReceiveRemoteNotification:`.
  329. * However, you only need to call this if you set the `FirebaseAppDelegateProxyEnabled`
  330. * flag to `NO` in your Info.plist. If `FirebaseAppDelegateProxyEnabled` is either missing
  331. * or set to `YES` in your Info.plist, the library will call this automatically.
  332. *
  333. * @param message The downstream message received by the application.
  334. *
  335. * @return Information about the downstream message.
  336. */
  337. - (FIRMessagingMessageInfo *)appDidReceiveMessage:(NSDictionary *)message;
  338. #pragma mark - GDPR
  339. /**
  340. * Deletes all the tokens and checkin data of the Firebase project and related data on the server
  341. * side. A network connection is required for the method to succeed.
  342. *
  343. * This does not delete the Firebase Installations ID. See `Installations.delete(completion:)`.
  344. * To prevent token auto generation, see `Messaging.isAutoInitEnabled`.
  345. *
  346. * @param completion A completion handler which is invoked when the operation completes. `error ==
  347. * nil` indicates success.
  348. */
  349. - (void)deleteDataWithCompletion:(void (^)(NSError *__nullable error))completion;
  350. @end
  351. NS_ASSUME_NONNULL_END