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