12
votes

I'm not quite sure how to interpret this sentences in the GCM Client documentation:

The android.permission.WAKE_LOCK permission so the application can keep the processor from sleeping when a message is received. Optional—use only if the app wants to keep the device from sleeping.

.

If you don't hold a wake lock while transitioning the work to a service, you are effectively allowing the device to go back to sleep before the work completes. The net result is that the app might not finish processing the GCM message until some arbitrary point in the future, which is not what you want.

and

Using WakefulBroadcastReceiver is not a requirement. If you have a relatively simple app that doesn't require a service, you can intercept the GCM message in a regular BroadcastReceiver and do your processing there.

I'm not quite sure if my app needs to hold a wakelock or not (or if it requires a service). The Push Notification part is quite important to the app and it should not be delayed for more than a few minutes. Is there a chance that the BroadcastReceiver gets suspended before receiving all the data?

1
GcmReceiver only started working without the wake lock permission (as mentioned by @RobMeeuwisse) in Google Play Services v8.4.0 (com.google.android.gms:play-services-gcm:8.4.0). Prior to v8.4.0, a crash occurs when using GcmReceiver without the wake lock permission. In v8.4.0 without the wake lock permission, there is a warning: "Missing wake lock permission, service start may be delayed" – user1652110

1 Answers

5
votes

Is there a chance that the BroadcastReceiver gets suspended before receiving all the data?

No. You will not get control until the entire 4K-or-less payload has been downloaded and is available to you.

However, onReceive() is called on the main application thread, and so if your work will take more than a millisecond or so, you should use WakefulBroadcastReceiver and an IntentService for that work. Or, if you prefer, use my WakefulIntentService and a regular BroadcastReceiver.