GoogleCloudPlatform / gradle-appengine-templates

Freemarker based templates that build with the gradle-appengine-plugin
438 stars 205 forks source link

register() is Deprecated #60

Open ItamarShDev opened 8 years ago

marcochin commented 8 years ago

From the docs it says to replace

if (gcm == null) {
     gcm = GoogleCloudMessaging.getInstance(context);
}
String regId = gcm.register(SENDER_ID);

with this:

String regId = InstanceID.getInstance(context)
                      .getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);

Edit: nvm this doesn't work. This first method although deprecated will allow device to receive msgs, but the 2nd method won't. So I must be doing something wrong here..

stanbar commented 8 years ago

I'm also looking for answer

ghost commented 8 years ago

Actually, the following works.

String regId = InstanceID.getInstance(context)
                      .getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);

Are you sure you've set up your _SENDERID constant properly? Its value should be your project's "Project number" which you can find in your Cloud Console.

stanbar commented 8 years ago

Ok, it works, but how to register device with this token to RegistrationEndpoint Api ?

ghost commented 8 years ago

You need to "build" an instance of it, then call the API's registerDevice() (or whatever you named the registration method). I believe it's also best to do so in an AsyncTask.

ghost commented 8 years ago

Here's an example implementation


RegistrationApi.Builder builder = new RegistrationApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory())
                    // options for running against local devappserver
                    // - 10.0.2.2 is localhost's IP address in Android emulator
                    // - turn off compression when running against local devappserver
                    .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
                    // end options for devappserver
builder.setApplicationName(APP_NAME);
RegistrationApi registrationApi = builder.build();

try {
      registrationApi.registerDevice(regId).execute();
} catch (IOException e) {
      Log.e(TAG, e.getMessage());
}