Search code examples
androidpush-notificationbroadcastreceivergoogle-cloud-messagingintentservice

Is there any reason to continue using IntentService for handling GCM messages?


As you know, recently Google changed their GCM documentation, and they claim that an IntentService is no longer required for handling arriving GCM messages. All the handling can be done in the BroadcastReceiver.

When trying to figure out if there is any good reason to continue using the IntentService, I came across this quote:

A Service (typically an IntentService) to which the WakefulBroadcastReceiver passes off the work of handling the GCM message, while ensuring that the device does not go back to sleep in the process. Including an IntentService is optional—you could choose to process your messages in a regular BroadcastReceiver instead, but realistically, most apps will use a IntentService.

Why would most apps use an IntentService? Are there any scenarios in which handling the GCM message directly in the BroadcastReceiver won't work?


Solution

  • Why would most apps use an IntentService?

    Because most likely whatever you are doing in response to the message will take more than 1-2ms, which means that you want to get that work off the main application thread. A common pattern for doing that in response to a broadcast is to delegate the work to an IntentService.

    So, if your work in response to the GCM message involves:

    • disk I/O
    • further network I/O (e.g., retrieving additional data from your Web service)
    • substantial calculations (e.g., image processing)

    you will likely want to use an IntentService to perform that work.