在Android中屏幕锁定时未调用BroadcastReceiver

Par*_*han 4 android broadcastreceiver push-notification google-cloud-messaging

在我的应用程序中,当通知到达时,BroadcastReceiver如果屏幕被锁定,则不会调用该通知.但是当屏幕解锁时,BroadcastReceiver会调用它并显示通知.

我还在我的清单中加入了以下权限:

android.permission.WAKE_LOCK
Run Code Online (Sandbox Code Playgroud)

但仍然没有工作.

Era*_*ran 15

这是适用于我的代码:

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);
Run Code Online (Sandbox Code Playgroud)

确保您从服务器发送通知delay_while_idle=0(这是默认值),或者在您的设备唤醒之前GCM不会发送通知.