Registration in Firebase Cloud Messaging


I am developing an app in Android , and I plan to implement downstream messages, the drawback is to get a valid token for registration in < em> FCM .

Although, I know that getting the token in this way is correct ...

GoogleCloudMessaging gcm= GoogleCloudMessaging.getInstance( context);
String regid=  gcm.register(SENDER_ID  );

... is obsolete. Is there another way to register but for the new version?

asked by Sonia Toledo 26.10.2016 в 01:25

1 answer


Well, the way I use with FCM is putting MainActivity in:

if (FirebaseInstanceId.getInstance().getToken()== null)

And then in the class that extends FirebaseInstanceIdService the following:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    // [START refresh_token]
    public void onTokenRefresh() {
                        // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Refreshed token: " + refreshedToken);

                new regInServer().execute(refreshedToken);
    // [END refresh_token]

Also, do not forget to include the services in the Manifest:

<service android:name=".MyFirebaseMessagingService">
        <action android:name="" />
<service android:name=".MyFirebaseInstanceIDService">
        <action android:name="" />

And I do not know if I forgot something, but I hope you're worth it.

answered by 04.11.2016 в 12:36