Question

I must get GCM registration ID when I start app at first time after registration GCM.

if I execute app from second time, I can get CGM registration ID(notify id). but when first time i start, I can't get it...

please tell me how to get?

this is my code:

private String GCMConnection() {
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    String notifyId = GCMRegistrar.getRegistrationId(this);
    if (notifyId.equals("")) {
        GCMRegistrar.register(this, GCMParam.SENDER_ID);
        notifyId = GCMRegistrar.getRegistrationId(this);
    }
    return notifyId;
}
Was it helpful?

Solution

GCMRegistrar.register is an asynchronous method. If you choose to use it, you can get the response via a broadcast receiver. GCMRegistrar.getRegistrationId returns a non empty value only after the first time your app gets a response to the registration request.

However, that class is deprecated. You are encouraged to use GoogleCloudMessaging.register instead, which is synchronous, and therefore shouldn't be run on the main thread.

You can see code samples in the official GCM guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top