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?
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.