我正在使用react-native-firebase v6
处理推送通知。问题是接收到推送通知时没有声音播放。
我已经创建了一个频道并将其设置为高优先级,因为react-native-firebase创建的默认频道没有声音。这是我在 MainActivity.java
中使用的代码protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("notifid", "App notification channel", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setShowBadge(true);
notificationChannel.setDescription("custom notification channel");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{400, 200, 400});
//notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
这是更改 firebase.json
中的频道的代码{
"react-native": {
"messaging_android_notification_channel_id": "notifid"
}
}
但是收到通知时声音仍然没有播放。
答案 0 :(得分:0)
您需要通过setSound方法设置声音。使用NotificationChannel类,您可以分别为每个通道设置声音,并在接收到特定通知时播放声音。 根据{{3}}
设置发布到此频道的通知应播放的声音及其音频属性。重要性至少为NotificationManager#IMPORTANCE_DEFAULT的通知通道应具有声音。只能在将通道提交到NotificationManager#createNotificationChannel(NotificationChannel)之前进行修改。
您可以使用系统声音,也可以将您的自定义声音放在res文件夹中,然后在方法签名中进行调用。 只需添加以下代码行即可设置声音:
notificationChannel.setSound("sound.mp3",AttributesToSet);
此处AttributesToSet引用docs类,您可以使用该类提供有关声音文件的更多信息。
更新: 要使用默认的通知声音,可以使用以下代码段。
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); //define this before building notification channel
然后在setSound方法中将此uri用作: notificationChannel.setSound(notification);