FIRInstanceID.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #import <Foundation/Foundation.h>
  2. NS_ASSUME_NONNULL_BEGIN
  3. @class FIRInstanceIDResult;
  4. /**
  5. * @memberof FIRInstanceID
  6. *
  7. * The scope to be used when fetching/deleting a token for Firebase Messaging.
  8. */
  9. FOUNDATION_EXPORT NSString *const kFIRInstanceIDScopeFirebaseMessaging
  10. NS_SWIFT_NAME(InstanceIDScopeFirebaseMessaging);
  11. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  12. /**
  13. * Called when the system determines that tokens need to be refreshed.
  14. * This method is also called if Instance ID has been reset in which
  15. * case, tokens and FCM topic subscriptions also need to be refreshed.
  16. *
  17. * Instance ID service will throttle the refresh event across all devices
  18. * to control the rate of token updates on application servers.
  19. */
  20. FOUNDATION_EXPORT const NSNotificationName kFIRInstanceIDTokenRefreshNotification
  21. NS_SWIFT_NAME(InstanceIDTokenRefresh);
  22. #else
  23. /**
  24. * Called when the system determines that tokens need to be refreshed.
  25. * This method is also called if Instance ID has been reset in which
  26. * case, tokens and FCM topic subscriptions also need to be refreshed.
  27. *
  28. * Instance ID service will throttle the refresh event across all devices
  29. * to control the rate of token updates on application servers.
  30. */
  31. FOUNDATION_EXPORT NSString *const kFIRInstanceIDTokenRefreshNotification
  32. NS_SWIFT_NAME(InstanceIDTokenRefreshNotification);
  33. #endif // defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  34. /**
  35. * @related FIRInstanceID
  36. *
  37. * The completion handler invoked when the InstanceID token returns. If
  38. * the call fails we return the appropriate `error code` as described below.
  39. *
  40. * @param token The valid token as returned by InstanceID backend.
  41. *
  42. * @param error The error describing why generating a new token
  43. * failed. See the error codes below for a more detailed
  44. * description.
  45. */
  46. typedef void (^FIRInstanceIDTokenHandler)(NSString *__nullable token, NSError *__nullable error)
  47. NS_SWIFT_NAME(InstanceIDTokenHandler);
  48. /**
  49. * @related FIRInstanceID
  50. *
  51. * The completion handler invoked when the InstanceID `deleteToken` returns. If
  52. * the call fails we return the appropriate `error code` as described below
  53. *
  54. * @param error The error describing why deleting the token failed.
  55. * See the error codes below for a more detailed description.
  56. */
  57. typedef void (^FIRInstanceIDDeleteTokenHandler)(NSError *error)
  58. NS_SWIFT_NAME(InstanceIDDeleteTokenHandler);
  59. /**
  60. * @related FIRInstanceID
  61. *
  62. * The completion handler invoked when the app identity is created. If the
  63. * identity wasn't created for some reason we return the appropriate error code.
  64. *
  65. * @param identity A valid identity for the app instance, nil if there was an error
  66. * while creating an identity.
  67. * @param error The error if fetching the identity fails else nil.
  68. */
  69. typedef void (^FIRInstanceIDHandler)(NSString *__nullable identity, NSError *__nullable error)
  70. NS_SWIFT_NAME(InstanceIDHandler);
  71. /**
  72. * @related FIRInstanceID
  73. *
  74. * The completion handler invoked when the app identity and all the tokens associated
  75. * with it are deleted. Returns a valid error object in case of failure else nil.
  76. *
  77. * @param error The error if deleting the identity and all the tokens associated with
  78. * it fails else nil.
  79. */
  80. typedef void (^FIRInstanceIDDeleteHandler)(NSError *__nullable error)
  81. NS_SWIFT_NAME(InstanceIDDeleteHandler);
  82. /**
  83. * @related FIRInstanceID
  84. *
  85. * The completion handler invoked when the app identity and token are fetched. If the
  86. * identity wasn't created for some reason we return the appropriate error code.
  87. *
  88. * @param result The result containing an identity for the app instance and a valid token,
  89. * nil if there was an error while creating the result.
  90. * @param error The error if fetching the identity or token fails else nil.
  91. */
  92. typedef void (^FIRInstanceIDResultHandler)(FIRInstanceIDResult *__nullable result,
  93. NSError *__nullable error)
  94. NS_SWIFT_NAME(InstanceIDResultHandler);
  95. /**
  96. * Public errors produced by InstanceID.
  97. */
  98. typedef NS_ENUM(NSUInteger, FIRInstanceIDError) {
  99. // Http related errors.
  100. /// Unknown error.
  101. FIRInstanceIDErrorUnknown = 0,
  102. /// Auth Error -- GCM couldn't validate request from this client.
  103. FIRInstanceIDErrorAuthentication = 1,
  104. /// NoAccess -- InstanceID service cannot be accessed.
  105. FIRInstanceIDErrorNoAccess = 2,
  106. /// Timeout -- Request to InstanceID backend timed out.
  107. FIRInstanceIDErrorTimeout = 3,
  108. /// Network -- No network available to reach the servers.
  109. FIRInstanceIDErrorNetwork = 4,
  110. /// OperationInProgress -- Another similar operation in progress,
  111. /// bailing this one.
  112. FIRInstanceIDErrorOperationInProgress = 5,
  113. /// InvalidRequest -- Some parameters of the request were invalid.
  114. FIRInstanceIDErrorInvalidRequest = 7,
  115. } NS_SWIFT_NAME(InstanceIDError);
  116. /**
  117. * A class contains the results of InstanceID and token query.
  118. */
  119. NS_SWIFT_NAME(InstanceIDResult)
  120. @interface FIRInstanceIDResult : NSObject <NSCopying>
  121. /**
  122. * An instanceID uniquely identifies the app instance.
  123. */
  124. @property(nonatomic, readonly, copy) NSString *instanceID;
  125. /*
  126. * Returns a Firebase Messaging scoped token for the firebase app.
  127. */
  128. @property(nonatomic, readonly, copy) NSString *token;
  129. @end
  130. /**
  131. * Instance ID provides a unique identifier for each app instance and a mechanism
  132. * to authenticate and authorize actions (for example, sending an FCM message).
  133. *
  134. * Once an InstanceID is generated, the library periodically sends information about the
  135. * application and the device where it's running to the Firebase backend. To stop this. see
  136. * `[FIRInstanceID deleteIDWithHandler:]`.
  137. *
  138. * Instance ID is long lived but, may be reset if the device is not used for
  139. * a long time or the Instance ID service detects a problem.
  140. * If Instance ID is reset, the app will be notified via
  141. * `kFIRInstanceIDTokenRefreshNotification`.
  142. *
  143. * If the Instance ID has become invalid, the app can request a new one and
  144. * send it to the app server.
  145. * To prove ownership of Instance ID and to allow servers to access data or
  146. * services associated with the app, call
  147. * `[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
  148. */
  149. NS_SWIFT_NAME(InstanceID)
  150. @interface FIRInstanceID : NSObject
  151. /**
  152. * FIRInstanceID.
  153. *
  154. * @return A shared instance of FIRInstanceID.
  155. */
  156. + (instancetype)instanceID NS_SWIFT_NAME(instanceID());
  157. /**
  158. * Unavailable. Use +instanceID instead.
  159. */
  160. - (instancetype)init __attribute__((unavailable("Use +instanceID instead.")));
  161. #pragma mark - Tokens
  162. /**
  163. * Returns a result of app instance identifier InstanceID and a Firebase Messaging scoped token.
  164. * param handler The callback handler invoked when an app instanceID and a default token
  165. * are generated and returned. If instanceID and token fetching fail for some
  166. * reason the callback is invoked with nil `result` and the appropriate error.
  167. */
  168. - (void)instanceIDWithHandler:(FIRInstanceIDResultHandler)handler;
  169. /**
  170. * Returns a Firebase Messaging scoped token for the firebase app.
  171. *
  172. * @return Returns the stored token if the device has registered with Firebase Messaging, otherwise
  173. * returns nil.
  174. */
  175. - (nullable NSString *)token __deprecated_msg("Use instanceIDWithHandler: instead.");
  176. /**
  177. * Returns a token that authorizes an Entity (example: cloud service) to perform
  178. * an action on behalf of the application identified by Instance ID.
  179. *
  180. * This is similar to an OAuth2 token except, it applies to the
  181. * application instance instead of a user.
  182. *
  183. * This is an asynchronous call. If the token fetching fails for some reason
  184. * we invoke the completion callback with nil `token` and the appropriate
  185. * error.
  186. *
  187. * This generates an Instance ID if it does not exist yet, which starts periodically sending
  188. * information to the Firebase backend (see `[FIRInstanceID getIDWithHandler:]`).
  189. *
  190. * Note, you can only have one `token` or `deleteToken` call for a given
  191. * authorizedEntity and scope at any point of time. Making another such call with the
  192. * same authorizedEntity and scope before the last one finishes will result in an
  193. * error with code `OperationInProgress`.
  194. *
  195. * @see FIRInstanceID deleteTokenWithAuthorizedEntity:scope:handler:
  196. *
  197. * @param authorizedEntity Entity authorized by the token.
  198. * @param scope Action authorized for authorizedEntity.
  199. * @param options The extra options to be sent with your token request. The
  200. * value for the `apns_token` should be the NSData object
  201. * passed to the UIApplicationDelegate's
  202. * `didRegisterForRemoteNotificationsWithDeviceToken` method.
  203. * The value for `apns_sandbox` should be a boolean (or an
  204. * NSNumber representing a BOOL in Objective C) set to true if
  205. * your app is a debug build, which means that the APNs
  206. * device token is for the sandbox environment. It should be
  207. * set to false otherwise. If the `apns_sandbox` key is not
  208. * provided, an automatically-detected value shall be used.
  209. * @param handler The callback handler which is invoked when the token is
  210. * successfully fetched. In case of success a valid `token` and
  211. * `nil` error are returned. In case of any error the `token`
  212. * is nil and a valid `error` is returned. The valid error
  213. * codes have been documented above.
  214. */
  215. - (void)tokenWithAuthorizedEntity:(NSString *)authorizedEntity
  216. scope:(NSString *)scope
  217. options:(nullable NSDictionary *)options
  218. handler:(FIRInstanceIDTokenHandler)handler;
  219. /**
  220. * Revokes access to a scope (action) for an entity previously
  221. * authorized by `[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
  222. *
  223. * This is an asynchronous call. Call this on the main thread since InstanceID lib
  224. * is not thread safe. In case token deletion fails for some reason we invoke the
  225. * `handler` callback passed in with the appropriate error code.
  226. *
  227. * Note, you can only have one `token` or `deleteToken` call for a given
  228. * authorizedEntity and scope at a point of time. Making another such call with the
  229. * same authorizedEntity and scope before the last one finishes will result in an error
  230. * with code `OperationInProgress`.
  231. *
  232. * @param authorizedEntity Entity that must no longer have access.
  233. * @param scope Action that entity is no longer authorized to perform.
  234. * @param handler The handler that is invoked once the unsubscribe call ends.
  235. * In case of error an appropriate error object is returned
  236. * else error is nil.
  237. */
  238. - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  239. scope:(NSString *)scope
  240. handler:(FIRInstanceIDDeleteTokenHandler)handler;
  241. #pragma mark - Identity
  242. /**
  243. * Asynchronously fetch a stable identifier that uniquely identifies the app
  244. * instance. If the identifier has been revoked or has expired, this method will
  245. * return a new identifier.
  246. *
  247. * Once an InstanceID is generated, the library periodically sends information about the
  248. * application and the device where it's running to the Firebase backend. To stop this. see
  249. * `[FIRInstanceID deleteIDWithHandler:]`.
  250. *
  251. * @param handler The handler to invoke once the identifier has been fetched.
  252. * In case of error an appropriate error object is returned else
  253. * a valid identifier is returned and a valid identifier for the
  254. * application instance.
  255. */
  256. - (void)getIDWithHandler:(FIRInstanceIDHandler)handler NS_SWIFT_NAME(getID(handler:));
  257. /**
  258. * Resets Instance ID and revokes all tokens.
  259. *
  260. * This method also triggers a request to fetch a new Instance ID and Firebase Messaging scope
  261. * token. Please listen to kFIRInstanceIDTokenRefreshNotification when the new ID and token are
  262. * ready.
  263. *
  264. * This stops the periodic sending of data to the Firebase backend that began when the Instance ID
  265. * was generated. No more data is sent until another library calls Instance ID internally again
  266. * (like FCM, RemoteConfig or Analytics) or user explicitly calls Instance ID APIs to get an
  267. * Instance ID and token again.
  268. */
  269. - (void)deleteIDWithHandler:(FIRInstanceIDDeleteHandler)handler NS_SWIFT_NAME(deleteID(handler:));
  270. @end
  271. NS_ASSUME_NONNULL_END