问题
我收到 GCM 通知,但是当我的屏幕被锁定或关闭时,我的振动和声音就不起作用了。 我尝试添加唤醒锁,但从 GCMListenerService 的外观来看,在触发通知时已经有唤醒 Intent 。 我应该怎么做才能让它发挥作用?我丢失的部分在哪里?
编辑
- 我的振动功能在屏幕开启和解锁时起作用。
- 我发现
GcmListenerService
的一部分负责接收 GCM 推送通知,看起来唤醒锁正在被删除(代码如下)
Bundle var2 = var1.getExtras();
var2.remove("消息类型");
var2.remove("android.support.content.wakelockid");
代码
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(getString(R.string.new_notification))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[] {1000, 1000, 1000})
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(10000);
notificationManager.notify(0, notification);
最佳答案
编辑
Google 现在对通知负载及其处理方式提供了合理的解释 -> Notification payloads .
===================
所以事实证明,当您的 Notification 包含 data payload
和 notification payload
然后 Android 在锁定模式下从播放负载创建通知,即使您已经编写了自定义代码。
不需要唤醒锁 - GcmListenerService
只要你有 <uses-permission android:name="android.permission.WAKE_LOCK" />
就可以自行决定是否需要
关于android - 通知振动,屏幕锁定时的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672133/