FIRMessagingTokenManager.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2019 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 "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  17. @class FIRMessagingAuthService;
  18. @class FIRMessagingCheckinPreferences;
  19. @class FIRMessagingTokenInfo;
  20. @protocol FIRHeartbeatLoggerProtocol;
  21. typedef NS_OPTIONS(NSUInteger, FIRMessagingInvalidTokenReason) {
  22. FIRMessagingInvalidTokenReasonNone = 0, // 0
  23. FIRMessagingInvalidTokenReasonAppVersion = (1 << 0), // 0...00001
  24. FIRMessagingInvalidTokenReasonAPNSToken = (1 << 1), // 0...00010
  25. };
  26. /**
  27. * Manager for the InstanceID token requests i.e `newToken` and `deleteToken`. This
  28. * manages the overall interaction of the `FIRMessagingTokenStore`, the token register
  29. * service and the callbacks associated with `GCMInstanceID`.
  30. */
  31. @interface FIRMessagingTokenManager : NSObject
  32. @property(nonatomic, readonly, copy) NSString *deviceAuthID;
  33. @property(nonatomic, readonly, copy) NSString *secretToken;
  34. @property(nonatomic, readonly, copy) NSString *versionInfo;
  35. @property(nonatomic, readonly, copy) NSString *defaultFCMToken;
  36. @property(nonatomic, readwrite, copy) NSString *fcmSenderID;
  37. @property(nonatomic, readwrite, copy) NSString *firebaseAppID;
  38. /// Expose the auth service, so it can be used by others
  39. @property(nonatomic, readonly, strong) FIRMessagingAuthService *authService;
  40. - (instancetype)init NS_UNAVAILABLE;
  41. /**
  42. * Designated intializer.
  43. *
  44. * @param heartbeatLogger The heartbeat logger that is injected into token operations.
  45. */
  46. - (instancetype)initWithHeartbeatLogger:(id<FIRHeartbeatLoggerProtocol>)heartbeatLogger
  47. NS_DESIGNATED_INITIALIZER;
  48. /**
  49. * Fetch new token for the given authorizedEntity and scope. This makes an
  50. * asynchronous request to the InstanceID backend to create a new token for
  51. * the service and returns it. This will replace any old token for the given
  52. * authorizedEntity and scope that has been cached before.
  53. *
  54. * @param authorizedEntity The authorized entity for the token, should not be nil.
  55. * @param scope The scope for the token, should not be nil.
  56. * @param instanceID The unique string identifying the app instance.
  57. * @param options The options to be added to the fetch request.
  58. * @param handler The handler to be invoked once we have the token or the
  59. * fetch request to InstanceID backend results in an error. Also
  60. * since it's a public handler it should always be called
  61. * asynchronously. This should be non-nil.
  62. */
  63. - (void)fetchNewTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  64. scope:(NSString *)scope
  65. instanceID:(NSString *)instanceID
  66. options:(NSDictionary *)options
  67. handler:(FIRMessagingFCMTokenFetchCompletion)handler;
  68. - (void)tokenWithAuthorizedEntity:(NSString *)authorizedEntity
  69. scope:(NSString *)scope
  70. options:(NSDictionary *)options
  71. handler:(FIRMessagingFCMTokenFetchCompletion)handler;
  72. /**
  73. * Return the cached token info, if one exists, for the given authorizedEntity and scope.
  74. *
  75. * @param authorizedEntity The authorized entity for the token.
  76. * @param scope The scope for the token.
  77. *
  78. * @return The cached token info, if available, matching the parameters.
  79. */
  80. - (FIRMessagingTokenInfo *)cachedTokenInfoWithAuthorizedEntity:(NSString *)authorizedEntity
  81. scope:(NSString *)scope;
  82. /**
  83. * Delete the token for the given authorizedEntity and scope. If the token has
  84. * been cached, it will be deleted from the store. It will also make an
  85. * asynchronous request to the InstanceID backend to invalidate the token.
  86. *
  87. * @param authorizedEntity The authorized entity for the token, should not be nil.
  88. * @param scope The scope for the token, should not be nil.
  89. * @param instanceID The unique string identifying the app instance.
  90. * @param handler The handler to be invoked once the delete request to
  91. * InstanceID backend has returned. If the request was
  92. * successful we invoke the handler with a nil error;
  93. * otherwise we call it with an appropriate error. Also since
  94. * it's a public handler it should always be called
  95. * asynchronously. This should be non-nil.
  96. */
  97. - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
  98. scope:(NSString *)scope
  99. instanceID:(NSString *)instanceID
  100. handler:(FIRMessagingDeleteFCMTokenCompletion)handler;
  101. /**
  102. * Deletes all cached tokens from the persistent store. This method should only be triggered
  103. * when InstanceID is deleted
  104. *
  105. * @param handler The handler to be invoked once the delete request to InstanceID backend
  106. * has returned. If the request was successful we invoke the handler with
  107. * a nil error; else we pass in an appropriate error. This should be non-nil
  108. * and be called asynchronously.
  109. */
  110. - (void)deleteAllTokensWithHandler:(FIRMessagingDeleteFCMTokenCompletion)handler;
  111. /**
  112. * Deletes all cached tokens from the persistent store.
  113. * @param handler The callback handler which is invoked when tokens deletion is complete,
  114. * with an error if there is any.
  115. *
  116. */
  117. - (void)deleteWithHandler:(void (^)(NSError *))handler;
  118. /**
  119. * Stop any ongoing token operations.
  120. */
  121. - (void)stopAllTokenOperations;
  122. /**
  123. * Invalidate any cached tokens, if the app version has changed since last launch or if the token
  124. * is cached for more than 7 days.
  125. * @param IID The cached instanceID, check if token is prefixed by such IID.
  126. *
  127. * @return Whether we should fetch default token from server.
  128. *
  129. * @discussion This should safely be called prior to any tokens being retrieved from
  130. * the cache or being fetched from the network.
  131. */
  132. - (BOOL)checkTokenRefreshPolicyWithIID:(NSString *)IID;
  133. /**
  134. * Upon being provided with different APNs or sandbox, any locally cached tokens
  135. * should be deleted, and the new APNs token should be cached.
  136. *
  137. * @discussion It is possible for this method to be called while token operations are
  138. * in-progress or queued. In this case, the in-flight token operations will have stale
  139. * APNs information. The default token is checked for being out-of-date by Instance ID,
  140. * and re-fetched. Custom tokens are not currently checked.
  141. *
  142. * @param deviceToken The APNS device token, provided by the operating system.
  143. * @param isSandbox YES if the device token is for the sandbox environment, NO otherwise.
  144. *
  145. * @return The array of FIRMessagingTokenInfo objects which were invalidated.
  146. */
  147. - (NSArray<FIRMessagingTokenInfo *> *)updateTokensToAPNSDeviceToken:(NSData *)deviceToken
  148. isSandbox:(BOOL)isSandbox;
  149. /*
  150. * Sets APNS token
  151. */
  152. - (void)setAPNSToken:(NSData *)APNSToken withUserInfo:(NSDictionary *)userInfo;
  153. - (BOOL)hasValidCheckinInfo;
  154. /*
  155. * Gets the current default token, if not exist, request a new one from server.
  156. */
  157. - (NSString *)tokenAndRequestIfNotExist;
  158. /*
  159. * Saves the default token to the keychain.
  160. */
  161. - (void)saveDefaultTokenInfoInKeychain:(NSString *)defaultFcmToken;
  162. /*
  163. * Posts a token refresh notification when a default FCM token is generated.
  164. *
  165. */
  166. - (void)postTokenRefreshNotificationWithDefaultFCMToken:(NSString *)defaultFCMToken;
  167. /*
  168. * Checks if two tokens have changed.
  169. */
  170. - (BOOL)hasTokenChangedFromOldToken:(NSString *)oldToken toNewToken:(NSString *)newToken;
  171. @end