Android 屏幕锁定时发出振动、声音通知

Android 屏幕锁定时发出振动、声音通知,android,google-cloud-messaging,android-notifications,Android,Google Cloud Messaging,Android Notifications,问题 我收到GCM通知,但是当我的屏幕被锁定或关闭时,我的振动和声音就不起作用了。 我曾尝试添加wakelock,但从GCMListenerService的外观来看,当触发通知时,已经存在唤醒意图。 我该怎么做才能让它工作?我丢失的那块在哪里 编辑 当屏幕打开和解锁时,我的振动工作 我发现部分负责接收GCM推送通知的GcmListenerService,看起来wakelock正在被删除(下面的代码) Bundle var2=var1.getExtras(); var2.删除(“消息类型”);

问题

我收到GCM通知,但是当我的屏幕被锁定或关闭时,我的振动和声音就不起作用了。 我曾尝试添加wakelock,但从GCMListenerService的外观来看,当触发通知时,已经存在唤醒意图。 我该怎么做才能让它工作?我丢失的那块在哪里

编辑

  • 当屏幕打开和解锁时,我的振动工作
  • 我发现部分负责接收GCM推送通知的
    GcmListenerService
    ,看起来wakelock正在被删除(下面的代码)
Bundle var2=var1.getExtras();
var2.删除(“消息类型”);
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);

您需要在服务器端代码上添加此标志
delay\u,而\u idle=true
。 请参见编辑

谷歌现在为通知有效载荷以及如何处理提供了合理的解释->

===================

因此,当您的通知包含
数据有效负载
通知有效负载
时,Android在锁定模式下从playload创建通知,即使您有自定义的代码


Wakelock是不需要的-
GcmListenerService
将自行决定是否需要Wakelock,只要您有
我正在使用FCM,经过大量的努力(我想是缺少文档),我通过从发送给FCM的字段中删除通知来解决问题,并且只使用了
数据。以下是我的php代码:

$fields = array(
    'to' => 'topics/all',
    'data' => array(
        'priority' => 'high',
        'title' => $title,
        'body' => $body,
        'type' => $type));

此外,您还必须将优先级设置为
,以使其在设备锁定时响铃并振动。

确保您已在清单文件中声明默认通知通道

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" 
           android:value="YOUR_CHANNEL_ID"/>


您是否添加了
@逻辑是,当然根据您发布的链接,当设备未接收到数据时,需要此标志。在我的例子中,我确实得到了通知和它的所有数据。唯一的问题是它的声音和振动没有被触发。是的,我理解你的意思,当设备处于睡眠模式或解锁时,你没有收到通知。我在服务器端设置了delay\u while\u idle=false,现在它在我的例子中按预期工作。