【问题标题】:React Native: Disable sound/vibration on push notificationReact Native:在推送通知上禁用声音/振动
【发布时间】:2019-07-13 22:47:33
【问题描述】:

我正在通过 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)

【问题讨论】:

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


    【解决方案1】:

    在设置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);
    

    【讨论】:

    • 嘿,我没有使用本地通知 :(
    • 如果是这样,可以在“Firebase 通知网络控制台”中进行设置。您可以在高级选项中设置sound = disabled
    • 您说的是 Firebase 控制台?我找不到那个选项。但是,我只希望某些通知没有声音/振动,而对于其他通知,我会想要声音
    • 它对我没有任何影响。检查我更新的问题
    【解决方案2】:

    我不确定您是如何设置推送通知的,但您可以将额外数据标记到 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多