Retrieve and store registration tokens As noted in our client setup guides, your app should retrieve this token at initial startup and save it to your app server alongside a timestamp. This timestamp must be implemented by your code and your servers, as it is not provided for you by FCM SDKs.
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.
The quick solution is to store it in sharedPrefs and add this logic to onCreate
method in your MainActivity or class which is extending Application.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, instanceIdResult -> {
String newToken = instanceIdResult.getToken();
Log.e("newToken", newToken);
getActivity().getPreferences(Context.MODE_PRIVATE).edit().putString("fb", newToken).apply();
});
Log.d("newToken", getActivity().getPreferences(Context.MODE_PRIVATE).getString("fb", "empty :("));
A better option is to create a service and keep inside a similar logic. Firstly create new Service
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("newToken", s);
getSharedPreferences("_", MODE_PRIVATE).edit().putString("fb", s).apply();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
public static String getToken(Context context) {
return context.getSharedPreferences("_", MODE_PRIVATE).getString("fb", "empty");
}
}
And then add it to AndroidManifest file
<service
android:name=".MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Finally, you are able to use a static method from your Service MyFirebaseMessagingService.getToken(Context);
Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken());
It's still working when you are using older firebase library than version 17.x.x
The team behind Firebase Android SDK change API a little bit. I've implemented "Token to Server" logic like this:
In my instance of FirebaseMessagingService:
public class FirebaseCloudMessagingService extends FirebaseMessagingService {
...
@Override
public void onNewToken(String token) {
// sending token to server here
}
...
}
Keep in mind that token is per device, and it can be updated by Firebase regardless of your login logic. So, if you have Login and Logout functionality, you have to consider extra cases:
Using new API, you can get token like this:
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
// send it to server
}
});
Good luck!
FirebaseInstanceId
class and it's method getInstanceId
are also deprecated. So you have to use FirebaseMessaging
class and it's getToken
method instead.
FirebaseMessaging.getInstance().getToken().addOnSuccessListener(token -> {
if (!TextUtils.isEmpty(token)) {
Log.d(TAG, "retrieve token successful : " + token);
} else{
Log.w(TAG, "token should not be null...");
}
}).addOnFailureListener(e -> {
//handle e
}).addOnCanceledListener(() -> {
//handle cancel
}).addOnCompleteListener(task -> Log.v(TAG, "This is the token : " + task.getResult()));
The method getToken()
is deprecated. You can use getInstanceId()
instead.
If you want to handle results when requesting instanceId(token)
, check this code.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> {
if (instanceIdResult != null) {
String token = instanceIdResult.getToken();
if (!TextUtils.isEmpty(token)) {
Log.d(TAG, "retrieve token successful : " + token);
}
} else{
Log.w(TAG, "instanceIdResult should not be null..");
}
}).addOnFailureListener(e -> {
//do something with e
}).addOnCanceledListener(() -> {
//request has canceled
}).addOnCompleteListener(task -> Log.v(TAG, "task result : " + task.getResult().getToken()));
Important information.
if google play service hung or not running, then fcm return token =
null
If play service working properly then FirebaseInstanceId.getInstance().getToken()
method returns token
Log.d("FCMToken", "token "+ FirebaseInstanceId.getInstance().getToken());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With