iLyas Dev iLyas Dev - 3 months ago 139
Android Question

Firebase Notification for Android, onTokenRefresh Method

Can anyone help me about notification with Firebase to learn good things. For me, I think the

method is not necessary when the token is updated.

In all cases, the notification will be sent to all the devices where the application is located.

What is the utility of

public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//I talked about this method. :)

private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.


A Token is used as a means to identify your device. In simple android app using FCM (like in the tutorial and firebase-chat), guessing from your code (correct me if I'm wrong), we don't need to send the token to server because we don't implement our server to send the message. We directly talk with google FCM server.

When we implementing our own server to send the notification message, we need to know with high confident that our server 'talk' with a valid device. So it use the token generated from device as identifier.

Server will send the notification with your token to FCM server (i.e Google Server), then the FCM Server will handle the sending process.

So you only need to construct sendRegistrationToServer(refreshedToken); when you want to authorize your app receiving notification from your server.

let's explain it more with CodePath Google Cloud Messaging tutorial:

Much of the heavy lifting in supporting push notifications on Android is facilitated by Google-powered connection servers. These Google servers provide an API for messages to be sent from your server and relay these messages to any Android/iOS devices authorized to receive them.

An Android device with Google Play Services will already have FCM client support available. For push notifications to be received, an app must first obtain a token by registering with a Google server:


This token then must be passed along to your server so that it can be used to send subsequent push notifications:


Push notifications can be received assuming your app has registered to listen for FCM-based messages:

enter image description here

In other words, in order to implement FCM, your app will need both a Google server and your own server. When your app gets a token from Google, it needs to forward this token to your server. This token should be persisted by the server so that it can be used to make API calls to the Google server. With this approach, your server and the Android device do not need to create a persistent connection and the responsibility of queuing and relaying messages is all handled by Google's servers.