android - 通知振动,屏幕锁定时的声音

标签 android google-cloud-messaging android-notifications

问题

我收到 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 payloadnotification payload然后 Android 在锁定模式下从播放负载创建通知,即使您已经编写了自定义代码。

不需要唤醒锁 - GcmListenerService只要你有 <uses-permission android:name="android.permission.WAKE_LOCK" /> 就可以自行决定是否需要

关于android - 通知振动,屏幕锁定时的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672133/

相关文章:

android - 如何最有效地将 git build 版本号与 eclipse 的 apk 版本联系起来?

android - 在Android中使用字符串/字节播放音频

android - 如何在 android 中启用和禁用 GCM?

ios - Firebase Cloud Messaging 通知未显示在 iOS 设备上(前台和后台)

android - 如何取消前台服务使用通知(滑动关闭)或清除所有通知?

java - 套接字流的 flush() 有多可靠?

android - 这段代码this@MainActivity是什么意思?

android - 谷歌云消息 : I have given my Android app the necessary permissions but it still gives an error when registering the device

Android - AlarmManager 不触发 BroadcastReceiver 以显示本地通知

java - 收到无法在 channel "channel_id_here"上发布通知的错误