FIRMessagingTokenManager.h 7.9 KB

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