Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver not called when screen locked in Android

In my Application, when a notification arrives, the BroadcastReceiver is not called if the screen is locked. But when screen is unlocked, the BroadcastReceiver is called and displays the notification.

I also put the following permission in my manifest:

android.permission.WAKE_LOCK

But still not working.

like image 837
Parag Chauhan Avatar asked Aug 28 '13 07:08

Parag Chauhan


1 Answers

Here's the code that works for me:

NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(...);
...
mManager.notify(0, notification);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);

Make sure that you send the notification from your server with delay_while_idle=0 (that's the default value), or the notification won't be sent by GCM until your device wakes up.

like image 180
Eran Avatar answered Sep 29 '22 23:09

Eran