Migrate from GCM 2.0 to GCM 3.0 InstanceID API

℡╲_俬逩灬. 提交于 2020-01-02 04:11:09

问题


Currently I have released my application on play store and in that project i am using

compile 'com.google.android.gms:play-services:7.0.0'

Google play services Library Version 7.0 to implement Push notification using Google cloud messaging in my project.

Google play services library version 7.0 uses old registration procedure using register() method

GoogleCloudMessaging.register(SENDER_ID);

But Googel documentation says.

GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens

InstanceID API includes in Google play services version 7.5 so i have to migrate from 7.0 to 7.5 or more. But application is already on play store which is using GCM register() method.

I have refer this link which explains how to use InstanceID but i can not find any migration guide to migrate from GCM register() to InstanceID

so how can i migrate from GCM register() method to InstanceID API ?

any help will be appreciated.


回答1:


You just have to implement the InstanceId-API, reregister the devices and store the new IDs on your server.

That´s how they explain migration for apps upgrading from C2DM to GCM here which where completly different services so this will work for upgrading to the latest GCM-API and InstanceId-API as well.

You should try if it is possible to mix InstanceIDs and old RegIds when sending notifications (I could not find any hint about that). If not you´ll have to save a boolean or whatever together with each ID stored on your server in order to be able to distinguish which method you should use to send a notification to a particular device.




回答2:


You should add GCM dependency in build.gradle of app.

compile 'com.google.android.gms:play-services-gcm:10.0.1'

Then in your activity you should add the piece of code inside Async Task or Thread,

String authorizedEntity = PROJECT_ID; // Project id from Google Developer Console
String scope = "GCM";
String token = InstanceID.getInstance(context).getToken(authorizedEntity,scope);

LoginActivity.Java:

public class LoginActivity extends AppCompatActivity{

private String deviceToken;

@Override protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.activity_login_new);
    ButterKnife.bind(this);

      if (!NetworkHelper.checkActiveInternet(this)) {
        Methodutils.messageWithTitle(LoginActivity.this, "No Internet Connection",
            "Please check your internet connection.", v -> finish());
      } else {
        new RegisterDevice(false).execute();
      }
    }
  }

private class RegisterDevice extends AsyncTask<String, String, String> {
    private ProgressDialog dialog;

    @Override protected void onPreExecute() {
      super.onPreExecute();

      dialog = new ProgressDialog(LoginActivity.this);
      dialog.setMessage("loading...");
      dialog.setCancelable(false);
      dialog.show();
    }

    @Override protected String doInBackground(String... params) {
      try {

        String authorizedEntity = AppConstants.APP_ID;
        String scope = "GCM";

        deviceToken = InstanceID.getInstance(LoginActivity.this).getToken(authorizedEntity, scope);

        Log.e("New Device Token - ", deviceToken != null ? deviceToken : "NA");
        if (deviceToken != null) {
          return deviceToken;
        }
      } catch (IOException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }

    @Override public void onPostExecute(String result) {
      super.onPostExecute(result);
      if (dialog.isShowing) {
        dialog.dismiss();
      }
      if (result == null) return;
    }
  }

}


来源:https://stackoverflow.com/questions/32835851/migrate-from-gcm-2-0-to-gcm-3-0-instanceid-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!