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

2

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

At what point is the FCM Token generated?

I ask this, because if it is generated before the User registers, how do I associate the Token with the User?

    
asked by anonymous 20.09.2016 / 17:07

1 answer

0

Translated official documentation: DOCUMENTATION

On the first startup of the application, the FCM SDK generates a registration token of the client application instance. To direct the application to single devices or create device groups, you will need to access this token.

Note: With each authentication, a new token is generated for the user. For this reason I write the token in SharedPreferences because it is only possible to access this token during authentication. As a suggestion, here's how I wrote the current token in SharedPreferences:

Add the service on the manifest :

    <service
        android:name=".Messaging.FirebaseIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

Class .Messaging.FirebaseIDService with token saving on 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 write to the firetime realtime database on a specific node for users, with UserID , UserName , UserEmail strong>, PhotoURL and etc.

    
08.06.2017 / 04:05