Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate from GCM 2.0 to GCM 3.0 InstanceID API

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.

like image 843
Rajesh Avatar asked Sep 26 '22 15:09

Rajesh


2 Answers

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.

like image 81
sschmid Avatar answered Sep 30 '22 06:09

sschmid


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;
    }
  }

}
like image 25
Nanda Gopal Avatar answered Sep 30 '22 07:09

Nanda Gopal