禁用声音以进行本地通知

时间:2019-01-24 19:18:44

标签: firebase react-native firebase-cloud-messaging react-native-firebase

我正在尝试使用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);

我希望显示的通知没有任何干扰。

1 个答案:

答案 0 :(得分:0)

根据我的经验,必须考虑两件事:

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

    const channel = new firebase.notifications.Android.Channel(   “ testNotification”,   “测试通知”,   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);
相关问题