FIRInstanceID.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #import <Foundation/Foundation.h>
  2. /**
  3. * @memberof FIRInstanceID
  4. *
  5. * The scope to be used when fetching/deleting a token for Firebase Messaging.
  6. */
  7. FOUNDATION_EXPORT NSString * __nonnull const kFIRInstanceIDScopeFirebaseMessaging;
  8. /**
  9. * Called when the system determines that tokens need to be refreshed.
  10. * This method is also called if Instance ID has been reset in which
  11. * case, tokens and FCM topic subscriptions also need to be refreshed.
  12. *
  13. * Instance ID service will throttle the refresh event across all devices
  14. * to control the rate of token updates on application servers.
  15. */
  16. FOUNDATION_EXPORT NSString * __nonnull const kFIRInstanceIDTokenRefreshNotification;
  17. /**
  18. * @related FIRInstanceID
  19. *
  20. * The completion handler invoked when the InstanceID token returns. If
  21. * the call fails we return the appropriate `error code` as described below.
  22. *
  23. * @param token The valid token as returned by InstanceID backend.
  24. *
  25. * @param error The error describing why generating a new token
  26. * failed. See the error codes below for a more detailed
  27. * description.
  28. */
  29. typedef void(^FIRInstanceIDTokenHandler)( NSString * __nullable token, NSError * __nullable error);
  30. /**
  31. * @related FIRInstanceID
  32. *
  33. * The completion handler invoked when the InstanceID `deleteToken` returns. If
  34. * the call fails we return the appropriate `error code` as described below
  35. *
  36. * @param error The error describing why deleting the token failed.
  37. * See the error codes below for a more detailed description.
  38. */
  39. typedef void(^FIRInstanceIDDeleteTokenHandler)(NSError * __nullable error);
  40. /**
  41. * @related FIRInstanceID
  42. *
  43. * The completion handler invoked when the app identity is created. If the
  44. * identity wasn't created for some reason we return the appropriate error code.
  45. *
  46. * @param identity A valid identity for the app instance, nil if there was an error
  47. * while creating an identity.
  48. * @param error The error if fetching the identity fails else nil.
  49. */
  50. typedef void(^FIRInstanceIDHandler)(NSString * __nullable identity, NSError * __nullable error);
  51. /**
  52. * @related FIRInstanceID
  53. *
  54. * The completion handler invoked when the app identity and all the tokens associated
  55. * with it are deleted. Returns a valid error object in case of failure else nil.
  56. *
  57. * @param error The error if deleting the identity and all the tokens associated with
  58. * it fails else nil.
  59. */
  60. typedef void(^FIRInstanceIDDeleteHandler)(NSError * __nullable error);
  61. /**
  62. * @enum FIRInstanceIDError
  63. */
  64. typedef NS_ENUM(NSUInteger, FIRInstanceIDError) {
  65. // Http related errors.
  66. /// Unknown error.
  67. FIRInstanceIDErrorUnknown = 0,
  68. /// Auth Error -- GCM couldn't validate request from this client.
  69. FIRInstanceIDErrorAuthentication = 1,
  70. /// NoAccess -- InstanceID service cannot be accessed.
  71. FIRInstanceIDErrorNoAccess = 2,
  72. /// Timeout -- Request to InstanceID backend timed out.
  73. FIRInstanceIDErrorTimeout = 3,
  74. /// Network -- No network available to reach the servers.
  75. FIRInstanceIDErrorNetwork = 4,
  76. /// OperationInProgress -- Another similar operation in progress,
  77. /// bailing this one.
  78. FIRInstanceIDErrorOperationInProgress = 5,
  79. /// InvalidRequest -- Some parameters of the request were invalid.
  80. FIRInstanceIDErrorInvalidRequest = 7,
  81. };
  82. /**
  83. * The APNS token type for the app. If the token type is set to `UNKNOWN`
  84. * InstanceID will implicitly try to figure out what the actual token type
  85. * is from the provisioning profile.
  86. */
  87. typedef NS_ENUM(NSInteger, FIRInstanceIDAPNSTokenType) {
  88. /// Unknown token type.
  89. FIRInstanceIDAPNSTokenTypeUnknown,
  90. /// Sandbox token type.
  91. FIRInstanceIDAPNSTokenTypeSandbox,
  92. /// Production token type.
  93. FIRInstanceIDAPNSTokenTypeProd,
  94. };
  95. /**
  96. * Instance ID provides a unique identifier for each app instance and a mechanism
  97. * to authenticate and authorize actions (for example, sending a GCM message).
  98. *
  99. * Instance ID is long lived but, may be reset if the device is not used for
  100. * a long time or the Instance ID service detects a problem.
  101. * If Instance ID is reset, the app will be notified via
  102. * `kFIRInstanceIDTokenRefreshNotification`.
  103. *
  104. * If the Instance ID has become invalid, the app can request a new one and
  105. * send it to the app server.
  106. * To prove ownership of Instance ID and to allow servers to access data or
  107. * services associated with the app, call
  108. * `[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
  109. */
  110. @interface FIRInstanceID : NSObject
  111. /**
  112. * FIRInstanceID.
  113. *
  114. * @return A shared instance of FIRInstanceID.
  115. */
  116. + (nonnull instancetype)instanceID NS_SWIFT_NAME(instanceID());
  117. /**
  118. * Unavailable. Use +instanceID instead.
  119. */
  120. - (nonnull instancetype)init __attribute__((unavailable("Use +instanceID instead.")));
  121. /**
  122. * Set APNS token for the application. This APNS token will be used to register
  123. * with Firebase Messaging using `token` or
  124. * `tokenWithAuthorizedEntity:scope:options:handler`. If the token type is set to
  125. * `FIRInstanceIDAPNSTokenTypeUnknown` InstanceID will read the provisioning profile
  126. * to find out the token type.
  127. *
  128. * @param token The APNS token for the application.
  129. * @param type The APNS token type for the above token.
  130. */
  131. - (void)setAPNSToken:(nonnull NSData *)token
  132. type:(FIRInstanceIDAPNSTokenType)type;
  133. #pragma mark - Tokens
  134. /**
  135. * Returns a Firebase Messaging scoped token for the firebase app.
  136. *
  137. * @return Null Returns null if the device has not yet been registerd with
  138. * Firebase Message else returns a valid token.
  139. */
  140. - (nullable NSString *)token;
  141. /**
  142. * Returns a token that authorizes an Entity (example: cloud service) to perform
  143. * an action on behalf of the application identified by Instance ID.
  144. *
  145. * This is similar to an OAuth2 token except, it applies to the
  146. * application instance instead of a user.
  147. *
  148. * This is an asynchronous call. If the token fetching fails for some reason
  149. * we invoke the completion callback with nil `token` and the appropriate
  150. * error.
  151. *
  152. * Note, you can only have one `token` or `deleteToken` call for a given
  153. * authorizedEntity and scope at any point of time. Making another such call with the
  154. * same authorizedEntity and scope before the last one finishes will result in an
  155. * error with code `OperationInProgress`.
  156. *
  157. * @see FIRInstanceID deleteTokenWithAuthorizedEntity:scope:handler:
  158. *
  159. * @param authorizedEntity Entity authorized by the token.
  160. * @param scope Action authorized for authorizedEntity.
  161. * @param options The extra options to be sent with your token request. The
  162. * value for the `apns_token` should be the NSData object
  163. * passed to UIApplication's
  164. * `didRegisterForRemoteNotificationsWithDeviceToken` method.
  165. * All other keys and values in the options dict need to be
  166. * instances of NSString or else they will be discarded. Bundle
  167. * keys starting with 'GCM.' and 'GOOGLE.' are reserved.
  168. * @param handler The callback handler which is invoked when the token is
  169. * successfully fetched. In case of success a valid `token` and
  170. * `nil` error are returned. In case of any error the `token`
  171. * is nil and a valid `error` is returned. The valid error
  172. * codes have been documented above.
  173. */
  174. - (void)tokenWithAuthorizedEntity:(nonnull NSString *)authorizedEntity
  175. scope:(nonnull NSString *)scope
  176. options:(nullable NSDictionary *)options
  177. handler:(nonnull FIRInstanceIDTokenHandler)handler;
  178. /**
  179. * Revokes access to a scope (action) for an entity previously
  180. * authorized by `[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]`.
  181. *
  182. * This is an asynchronous call. Call this on the main thread since InstanceID lib
  183. * is not thread safe. In case token deletion fails for some reason we invoke the
  184. * `handler` callback passed in with the appropriate error code.
  185. *
  186. * Note, you can only have one `token` or `deleteToken` call for a given
  187. * authorizedEntity and scope at a point of time. Making another such call with the
  188. * same authorizedEntity and scope before the last one finishes will result in an error
  189. * with code `OperationInProgress`.
  190. *
  191. * @param authorizedEntity Entity that must no longer have access.
  192. * @param scope Action that entity is no longer authorized to perform.
  193. * @param handler The handler that is invoked once the unsubscribe call ends.
  194. * In case of error an appropriate error object is returned
  195. * else error is nil.
  196. */
  197. - (void)deleteTokenWithAuthorizedEntity:(nonnull NSString *)authorizedEntity
  198. scope:(nonnull NSString *)scope
  199. handler:(nonnull FIRInstanceIDDeleteTokenHandler)handler;
  200. #pragma mark - Identity
  201. /**
  202. * Asynchronously fetch a stable identifier that uniquely identifies the app
  203. * instance. If the identifier has been revoked or has expired, this method will
  204. * return a new identifier.
  205. *
  206. *
  207. * @param handler The handler to invoke once the identifier has been fetched.
  208. * In case of error an appropriate error object is returned else
  209. * a valid identifier is returned and a valid identifier for the
  210. * application instance.
  211. */
  212. - (void)getIDWithHandler:(nonnull FIRInstanceIDHandler)handler;
  213. /**
  214. * Resets Instance ID and revokes all tokens.
  215. */
  216. - (void)deleteIDWithHandler:(nonnull FIRInstanceIDDeleteHandler)handler;
  217. @end