这段代码会在4月10日之后停止工作吗? GCM弃用

问题描述 投票:0回答:1

这个代码

public class PushInstanceIDListenerService extends InstanceIDListenerService implements PushConstants {
public static final String LOG_TAG = "Push_InstanceIDListener";

@Override
public void onTokenRefresh() {
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
    String senderID = sharedPref.getString(SENDER_ID, "");
    if (!"".equals(senderID)) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
    }
}

4月10日GCM弃用后停止工作?

google-cloud-messaging
1个回答
1
投票

是的,最好将代码迁移到FCM。请检查documentation

更改MyInstanceIDListenerService以扩展FirebaseInstanceIdService,并更新代码以侦听令牌更新,并在生成新令牌时获取令牌。

MyInstanceIDListenerService.java之前

public class MyInstanceIDListenerService extends InstanceIDListenerService {

  ...

  @Override
  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);
      startService(intent);
  }
}

MyInstanceIDListenerService.java之后

public class MyInstanceIDListenerService 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 InstanceID token is initially generated, so this is where
   * you retrieve the token.
   */
  // [START refresh_token]
  @Override
  public void onTokenRefresh() {
      // Get updated InstanceID token.
      String refreshedToken = FirebaseInstanceId.getInstance().getToken();
      Log.d(TAG, "Refreshed token: " + refreshedToken);
      // TODO: Implement this method to send any registration to your app's servers.
      sendRegistrationToServer(refreshedToken);
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.