Registration in Firebase Cloud Messaging

2

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
source

1 answer

1

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

if (FirebaseInstanceId.getInstance().getToken()== null)
{
    FirebaseMessaging.getInstance().subscribeToTopic("news");
}

And then in the class that extends FirebaseInstanceIdService the following:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    // [START refresh_token]
    @Override
    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">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

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

    
answered by 04.11.2016 в 12:36