React Native:在推送通知时禁用声音/振动

时间:2019-07-13 22:47:33

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

我正在通过Google FCM从服务器向客户端发送推送通知。

在react-native应用程序中,我注册了以下侦听器:

this.notificationOpenedListener = firebase.notifications().onNotificationOpened(async (notificationOpen) => {

})

this.notificationListener = firebase.notifications().onNotification(async (notification) => {

});

notification数据包含接收通知时是否应该发出声音/振动的信息。

但是,我找不到有关完全按需禁用声音/振动的任何文档。

我该如何实现?

更新

我尝试在服务器端将声音设置为空字符串,但是通知中仍然有声音/振动。

  var message = {
    data: {
      userId: fromUserId,
    },
    notification: {
      title: `My notifcation`,
      body: `Body`,
      sound: "",
    },
  }

  return sendToTopic(topicId, message)

2 个答案:

答案 0 :(得分:0)

设置notification目标时,请删除sound参数。

const notify = {
     ...message.notification,
     show_in_foreground: true,
     notify_type: message.NotificationType,
     obj_id: message.ObjectId,
     sound: '' // remove this
};
firebase.messaging().createLocalNotification(notify);

答案 1 :(得分:0)

我不确定您如何设置Push通知,但是您可以像这样将额外的数据标记到JSON中:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}

这意味着您可以向其添加一个布尔值,以在到达时手动播放音调,或者在订阅触发时不播放音调,如下所示:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark",
      "playTone" : "true"
    }
  }
}

然后是在回调中进行检查以检查响应的情况:


this.notificationListener = firebase.notifications().onNotification(async (notification) => {
 if(notification.data.playtone) {
     // Play Tone
     } else {
     // Don't
     }
});

通常,虽然推送通知是由操作系统而不是应用程序处理的,但是您可以像使用挂钩通知一样将其挂接到系统中,并在收到推送通知时执行操作,但是通常它们都以相同的方式“丢弃所有内容并处理”我”风格。

Android和Apple均支持优先级,但您可能并不需要这样做: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message

相关问题