Naga
Naga

Reputation: 2021

How to do GCM Registration from broadcastReceiver?

I am calling GCMRegistrar.register(context, Constants.GCM_SENDER_ID) from a BroadcastReceiver(Boot complete receiver) but it throws ReceiverCallNotAllowedException then I started a seprate thread from broadcastreceiver and from thread I called GCMRegistrar.register(context, Constants.GCM_SENDER_ID) again I got the same exception.

Then I started an IntentService from broadcastreceiver and from intent service I called

GCMRegistrar.register(context, Constants.GCM_SENDER_ID)

then I got sending message to a handler on a dead thread which comes because for android 2.3 it uses google accounts and I think it does some async task.

Now is there any way to achieve GCMRegistrar.register(context, Constants.GCM_SENDER_ID) successfully from a broadcastreceiver

Upvotes: 2

Views: 637

Answers (2)

Eran
Eran

Reputation: 393781

You could switch to using the new GoogleCloudMessaging class, as was suggested by StinePike, but GCMRegistrar.register still works. I guess that for some reason you can't use it from a broadcast receiver, but you can call it (for example) from your main activity. Is there a special reason you need to call it from a broadcast receiver?

Upvotes: 0

stinepike
stinepike

Reputation: 54672

GCMRegistrar is deprecated. The better alternative is to use GoolgeCloudMessaging class.

To register using this you can call

 String id = GoogleCloudMessaging.getInstance(context).register(senderId);

Upvotes: 2

Related Questions