Android通知没有声音和震动

5

我在我的应用程序中使用了FCM通知,我可以接收它们,并且标题和消息显示得很完美。

但是,当我接收通知时,我没有收到任何声音、震动或任何LED指示灯。

我的通知构建器代码如下:

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.drawable.applogo);
    builder.setContentTitle(Title);
    builder.setContentText(Message);
    builder.setAutoCancel(true);
    builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));
    builder.setContentIntent(pendingIntent);
    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setLights(Color.RED, 1000, 300);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, builder.build());

提前致谢。


你在 Android Manifest 中添加了振动权限吗? - Omkar
4个回答

5

1. 当你从firebase发送推送通知时,请确保启用了声音选项

像这样 enter image description here

2. 同样,请确保在Android清单中添加了震动权限。

<uses-permission android:name="android.permission.VIBRATE" />

3. 如果您是从服务器发送通知,请使用有效负载

在通知的通知有效负载中,有一个声音键。

根据官方文档,它的用途是:

指示设备在接收到通知时播放的声音。支持默认值或打包在应用程序中的声音资源的文件名。声音文件必须驻留在 /res/raw/ 中。

{
    "to" : ".......",

    "notification" : {
      "body" : "body",
      "title" : "title",
      "icon" : "myicon",
      "sound" : "default"
    }
  }

我正在使用Php从Web服务器发送通知。 - GuruCharan

0

试试这个

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

0

我认为你需要在清单文件中添加以下权限

<uses-permission android:name="android.permission.VIBRATE" />

问题还是存在,但已经添加了。 - GuruCharan

0
Android 通知没有声音和震动。尝试使用默认的 RingtoneManager
删除 builder.setSound(Uri.parse("file:///android_asset/notification.mp3"));
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);

在 Android 清单中添加了振动权限。

<uses-permission android:name="android.permission.VIBRATE" />

重要提示: 请确保云服务器端(GCM/Firebase)正确配置 Android 推送通知服务

确认您正获取下行(JSON)的正确有效负载

 {
      "to": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
      "notification": {
        "body": "great match!",
        "title": "SamDev Test",
        "icon": "myicon",
        "sound": "default"
      }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接