1
votes

I am trying to create an android app which can receive push notifications. I have already got my device registered and obtained the GCM id.

However I am confused if I need to implement a service to listen to the push notifications ? I am aware that I need to implement a broadcast receiver to process the push notification. However I am not sure if service is required in my application.

My aim is to create an application which I can start/stop remotely. I intent to do it via push notifications.

2
In the new implementation you will subclass GcmListenerService to listen for messages. Is that what you are following right now?Law Gimenez
yes I have implement a class which extends GcmListenerService. Within it there is this method onMessageReceived(String from, Bundle data). I know I have to override it. Does this mean that there is not need for a service and only a broadcast receiver will suffice ?Riju Baral
Yes, that GcmListenerService is enough for receiving push notifications. The other thing is when your GCM token needs to be refreshed or changed.Law Gimenez
@LawGimenez :- thanks for your inputs. But i didnt understand what do you mean by " when your GCM token needs to be refreshed or changed. – Law Gimenez 40 mins ago".Riju Baral
Sorry, what I meant was extending the class InstanceIDListenerService, but I haven't yet dig deeper on it though.Law Gimenez

2 Answers

1
votes

No, if you are using GcmListenerService like demonstrated here, the listening for incoming messages is handled for you.

So you do need a service, GcmListenerService is that service, but you don't need to write it yourself, just extend it and override onMessageReceived. You must still add the required declarations in your manifest though, and like Riju mentioned you should also extend InstanceIDListenerService to handle refreshed tokens, which is also demonstrated in the sample I liked to.

0
votes

No, you don't need to implement a service. Only a broadcast receiver is enough to get messages. You can start application after receive a message via GCM.