首页 > 解决方案 > 禁用本地通知声音

问题描述

我正在尝试使用 react-native-firebase 显示没有声音的本地通知,这可能吗?

我尝试过使用 AndroidNotification 类,但找不到这样做的方法。

  const notification = new firebase.notifications.Notification()
    .setNotificationId("notificationId")
    .setTitle("Title")
    .setBody("Text")
    .android.setBigText("Big Text")
    .android.setColor("#f04e29")
    .android.setAutoCancel(true)
    .android.setOnlyAlertOnce(true)
    .android.setChannelId("channel")
    .android.setLargeIcon("ic_launcher")
    .android.setSmallIcon("ic_notification");
  firebase.notifications().displayNotification(notification);

我希望通知显示时没有任何噪音。

标签: firebasereact-nativefirebase-cloud-messagingreact-native-firebase

解决方案


根据我自己的经验,必须考虑两点:

  1. 无需调用 setSound 方法或不设置声音属性
  2. 在为 Android API 级别 >=26 进行开发时,作为 Channel 方法的第三个参数传递的 Android 重要性必须设置为 Low、Min 或 None。否则无论如何都会播放默认声音。所以一个创建你的频道的例子(例如在你的 App.tsx 的 componentDidMount 中):

    const channel = new firebase.notifications.Android.Channel("testNotification", "Test Notification", firebase.notifications.Android.Importance.Low) firebase.notifications().android.createChannel(channel);

然后显示一个不显眼的通知:

const notification = new firebase.notifications.Notification()
  .setTitle("My notification title")
  .setBody("My notification body");
notification.android.setChannelId("testNotification");
firebase.notifications().displayNotification(notification);

推荐阅读