12

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?

Johann Bauer
  • 2,488
  • 2
  • 26
  • 41
  • 4
    `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 May 01 '16 at 23:49

1 Answers1

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    Well it will just show a notification, but I assume even that takes more than a millisecond. What exactly would happen if the intent that has been started is sent to sleep before it would display the notification? Would it disappear forever or would it continue it's work as soon as the phone wakes again? – Johann Bauer Mar 20 '14 at 20:06
  • 1
    @jreuab: "Well it will just show a notification, but I assume even that takes more than a millisecond" -- your code portion of it should not, unless you are doing some disk I/O to retrieve data to show in the `Notification`. "Would it disappear forever or would it continue it's work as soon as the phone wakes again?" -- it should continue as soon as the phone wakes up again. There is no guarantee how long that will be, though, particularly on devices set up for fairly aggressive power savings. – CommonsWare Mar 20 '14 at 20:09
  • So if we are using GCM's broadcast receiver i.e com.google.android.gcm.GCMBroadcastReceiver then we do not need this wake lock permission as we are not doing anything in onRecieve. – varun bhardwaj Jun 09 '15 at 06:57
  • As Google said in a post that For existing apps that extend a WakefulBroadcastReceiver, Google recommends migrating to GCMReceiver and GcmListenerService. So we should move to com.google.android.gcm.GCMBroadcastReceiver in any case, right? – varun bhardwaj Jun 09 '15 at 06:59
  • 1
    @varunbhardwaj: GCM does not have a `com.google.android.gcm.GCMBroadcastReceiver`, listed in their documentation. They do have [a `GcmListenerService`](https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmListenerService) and [a `GcmReceiver`](https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmReceiver), though they seem to be new, and I have not played with them yet. – CommonsWare Jun 09 '15 at 10:19
  • When GcmListenerService starts - it makes a call to "completeWakefulIntent" so it keeps the device awake and your code won't get suspended. – FunkSoulBrother Jan 17 '16 at 14:20
  • 2
    `GcmReceiver` can work with and without wake locks. It checks if the app has the `WAKE_LOCK` permission to decide wether or not to acquire a wake lock before starting the service. `GcmListenerService` calls `GcmReceiver.completeWakefulIntent()` when it is done, which releases the wake lock if it was acquired. – Rob Meeuwisse Mar 01 '16 at 11:46