What Is Fcm Token in Firebase

What is FCM token in Firebase?

What is it exactly?

An FCM Token, or much commonly known as a registrationToken like in google-cloud-messaging. As described in the GCM FCM docs:

An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.


How can I get that token?

Update: The token can still be retrieved by calling getToken(), however, as per FCM's latest version, the FirebaseInstanceIdService.onTokenRefresh() has been replaced with FirebaseMessagingService.onNewToken() -- which in my experience functions the same way as onTokenRefresh() did.


Old answer:

As per the FCM docs:

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.

You can access the token's value by extending FirebaseInstanceIdService. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh, and log the value as shown:

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}

The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. FirebaseInstanceID.getToken() returns null if the token has not yet been generated.

After you've obtained the token, you can send it to your app server and store it using your preferred method. See the Instance ID API reference for full detail on the API.

What's the difference : FCM Token / APNs Token / Registration Token / Device Token

There are two definitives here:

  • APNS Token is the Apple Push Notification Service token. It is a token (think of it like a password) that authenticates your app and device onto the Apple Push service and allows for communications to be sent.
  • FCM Token is the Firebase Cloud Messaging token. This is googles version of the APNS Token however works for both iOS and Android (Google do proxying on their end when sending a push notification to iOS devices).

When you refer to a registration token or a device token it really does depend on the context you use the phrases. They are often used interchangeably and neither are really "official" terms. For the most part, both terms refer to your APNS/FCM token, however device token has historically been used for other things like unique device identifiers.

Is it ok to have the firebase FCM initialization not at startup but after authentication?

You can do it for sure. I have used FCM in one of my project where if user is logged in I save the token but if not I first ask the user to login and after that I use to save the device token. And this should be used only, otherwise the messages receive on the device even if the user is not logged in to the app, and it seems to be very weird.

Note: Remember to delete the token as well if the user is logged out from the application.

Firebase id token generation is broken

I finally fixed this going to F12 (developer tools) > Application > Storage > Clear site data.
Firebase now generate a new valid token that works for calling API functions.



Related Topics



Leave a reply



Submit