Official documentation translated : DOCUMENTATION
On the first startup of the application, the FCM SDK generates a registry token of the client application instance. To direct the application to single devices or create groups of devices, you will need to access this token.
Note: At each authentication, a new token is generated for the user. For this reason I save the token to Sharedpreferences as it is only possible to have access to this token during authentication. As a suggestion, follow below how I recorded the current token in Sharedpreferences:
Add the service on manifesto:
<service
android:name=".Messaging.FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Class .Messaging.Firebaseidservice with the recording of the token in the Sharedpreferences:
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = SP.edit();
editor.putString("CfgTokenFCM", token);
editor.apply();
}
}
Ready. Now you can read the token in any class and when you want to :
// Read CfgTokenFCM
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
cfgTokenFCM = SP.getString("CfgTokenFCM", "");
I particularly still record on firebase Realtime database at a specific node for users, with Userid, Username, Useremail, Photourl and etc.