Token FCM (google) associate to User, at what time?

Asked

Viewed 634 times

2

An app that has User Registration and Login, consequently an ID for each user.

When the FCM Token is generated ?

I implementing within: onTokenRefresh() , the Token can be generated before I have a User with ID to associate...

I ask this because, if it is generated before the User registers, as I associate the Token to the User ?

1 answer

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.