On April 10, 2018, Google deprecated GCM. The GCM server and client APIs were removed on May 29, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features. See the migration guide to learn more.

Migrate your IID Listener Service

A custom service for token management (previously, one extending InstanceIDListenerService) is no longer required and should be removed. If you want to access the FCM token instead override the onNewToken method on FirebaseMessagingService.

This is needed if you want to

  • Manage device tokens to send a messages to single device directly, or

  • Send messages to device groups, or

  • Subscribe devices to topics with the server subscription management API.

If you don't use these features, you can completely remove your client code to initiate the generation of a registration token.

Registration token best practices for FCM

Send requests to GCM tokens will continue to work until the same instance registers using a new version of the FCM SDK. At that time, a new token may be returned that replaces all previous tokens, and previous tokens for that instance may be invalidated. In some situations a registration request will return the same token as an older registration, but you should not rely on that behavior; instead, implement onNewToken() to store only the latest valid token. The last token generated is the only "safe" token that is guaranteed to work, so you should replace any other tokens associated with the same instance.

Update the Android Manifest


        <action android:name="com.google.android.gms.iid.InstanceID" />


        <action android:name="com.google.firebase.MESSAGING_EVENT" />

Migrate your Instance ID listener service

Change MyInstanceIDListenerService to extend FirebaseMessagingService, and update code to listen for token updates and get the token whenever a new token is generated. This is the same class used for receiving messages. If you are also migrating a class extending GcmListenerService these will need to be combined into a single class.

MyInstanceIDListenerService.java Before

public class MyInstanceIDListenerService extends InstanceIDListenerService {


  public void onTokenRefresh() {
      // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
      Intent intent = new Intent(this, RegistrationIntentService.class);

MyFcmListenerService.java After

public class MyFcmListenerService extends FirebaseInstanceIdService {


   * Called if InstanceID token is updated. This may occur if the security of
   * the previous token had been compromised. Note that this is also called
   * when the Instance ID token is initially generated, so this is where
   * you retrieve the token.
  // [START refresh_token]
  public void onNewToken(String token) {
      Log.d(TAG, "New token: " + token);
      // TODO: Implement this method to send any registration to your app's servers.


Remove registration

You no longer need to explicitly initiate the generation of a registration token—the library does this automatically. Therefore, you can remove code like the following:

  InstanceID instanceID = InstanceID.getInstance(this);
  String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
          GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
  // [END get_token]
  Log.i(TAG, "GCM Registration Token: " + token);

Note that you still can retrieve the current token in FCM by using FirebaseInstanceId.getInstance().getInstanceId();.

Gửi phản hồi về...