【问题标题】:No sound when receiving push notification收到推送通知时没有声音
【发布时间】:2020-04-24 03:29:00
【问题描述】:

我正在使用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"
  }
}

但收到通知时声音仍然没有播放。

【问题讨论】:

  • 你能说明你在哪里处理通知对象吗?
  • @bored 我认为这与它的处理方式无关。收到推送通知时没有声音。所以它必须在 rn-firebase 设置或 android 端。

标签: android firebase react-native react-native-android react-native-firebase


【解决方案1】:

您需要通过 setSound 方法设置声音。使用 NotificationChannel 类,您可以分别为每个通道设置声音,并在收到特定通知时播放。 根据docs

设置应为发布到此频道及其音频属性的通知播放的声音。重要性至少为 NotificationManager#IMPORTANCE_DEFAULT 的通知通道应该有声音。只能在频道提交到 NotificationManager#createNotificationChannel(NotificationChannel) 之前修改。

您可以使用系统声音或将自定义声音放在 res 文件夹中,并在方法签名中相应地调用它。 只需添加以下代码行来设置声音:

notificationChannel.setSound("sound.mp3",AttributesToSet);

这里的 AttributesToSet 指的是 AudioAttributes 类,您可以使用它来提供有关您的声音文件的更多信息。

更新: 要使用默认通知声音,您可以使用以下 sn-p。

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); //define this before building notification channel

然后在 setSound 方法中使用这个 uri: notificationChannel.setSound(notification);

【讨论】:

  • 我缺乏android原生端,如果我想使用默认通知,我应该使用什么常量?
  • 这个频道的东西是否可编辑,我尝试将优先级更改为默认值并设置通知声音。但是安装后,通知设置中仍然禁用允许声音:-/
  • 要使事情同步,只需确保在引入/更改本机代码时重新启动打包程序并重新安装应用程序。
猜你喜欢
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
相关资源
最近更新 更多