Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is FCM token in Firebase?

In the new Firebase, under Notification, they have mentioned that developer can send notification to a particular device. For that, in console it asks for an FCM token. What is it exactly and how can I get that token?

like image 384
arunrk Avatar asked Jun 07 '16 05:06

arunrk


People also ask

What is FCM token Firebase?

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.

Is FCM token unique?

Yes, they are unique but they are not constant.

How can I check my FCM token?

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters. Notice that I added in "ABC" , in the registration_ids parameter.


1 Answers

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.

like image 74
AL. Avatar answered Sep 20 '22 22:09

AL.