Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Refresh Token

Using the method

[FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

Im not quite sure what the parameters are calling for? What is the authorized entity and action? Also do I pass in the APNS token from apple to that method?

like image 361
mKane Avatar asked Jul 18 '16 19:07

mKane


People also ask

How does firebase refresh token work?

Every time a user signs in, the user credentials are sent to the Firebase Authentication backend and exchanged for a Firebase ID token (a JWT) and refresh token. Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens.

How long does a firebase token last?

The Firebase Admin SDK has a built-in method for creating custom tokens. At a minimum, you need to provide a uid , which can be any string but should uniquely identify the user or device you are authenticating. These tokens expire after one hour.

How do you refresh a firebase token in flutter?

It only refreshes when : When user Uninstall/Reinstall the app or Clears App Data. You manually delete FCM Instance using FirebaseMessaging(). deleteInstanceID()


2 Answers

  1. AUTHORIZED_ENTITY - Basically it asks for the google project id. It is numeric, and if you already had GCM integrated in your project before, it would be GCM_SENDER_ID (something like "568520103762"). Check your Google-info.plist to find it.
  2. SCOPE - kFIRInstanceIDScopeFirebaseMessaging
  3. OPTIONS - @{@"apns_token": deviceToken} (You will get DeviceToken in didRegisterForRemoteNotifications method)
  4. HANDLER - Catch token if you have received token or catch the error here. If token comes nil, then wait for token in "tokenRefreshNotification" method, which will be called automatically if the token is nil in [FIRInstanceID tokenWithAuthorizedEntity:scope:options:handler]

Example:

 if (![[FIRInstanceID instanceID] token]) {
    [[FIRInstanceID instanceID] tokenWithAuthorizedEntity:_gcmSenderId scope:kFIRInstanceIDScopeFirebaseMessaging options:_registrationOptions handler:^(NSString * _Nullable token, NSError * _Nullable error) {

        // Fetch the token or error
    }];

}
like image 50
HeadOnn Avatar answered Sep 28 '22 05:09

HeadOnn


You can do like this.

[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];

[[FIRInstanceID instanceID] tokenWithAuthorizedEntity:gcmSenderID scope:kFIRInstanceIDTokenRefreshNotification options:nil handler:^(NSString * _Nullable token, NSError * _Nullable error) {

    NSLog(@"GCM Registration token = %@",token);
    NSLog(@"GCM Registration error = %@",error);        
}];
like image 38
Maheep Avatar answered Sep 28 '22 04:09

Maheep