我正在通过谷歌 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)
将推送通知添加到 React Native 项目可能很耗时并且需要大量调整。确保正确执行每个步骤,因为跳过一行可能会导致调试数小时。
点击此链接 - 推送通知
设置
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);
我不确定您是如何设置推送通知的,但您可以像这样将额外数据标记到 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