1

我使用 react-native-firebase 构建了一个简单的 react-native 应用程序。我成功地向我的设备发送了推送通知,然后我想实现如果用户触摸(单击)来自设备的通知,它将重定向到我附加的网页。

我安装了 fcm-node 来发送推送通知。代码如下

const FCM = require('fcm-node');

const message = {
    notification: {
        fcmTitle,
        body
    },
    to: token
};


 fcm.send(message, function (err, response) {
    console.log('response',response);
    console.log("error : "+err);
 })

因此,如果用户单击推送通知,它应该打开网络浏览器并重定向到我从代码中附加的页面

4

2 回答 2

1

这由 Firebase SDK 处理。在这里,当用户点击通知时,您的应用会收到与该通知关联的有效负载。

请看看如何处理它

async createNotificationListeners() {
/*
 Triggered when a particular notification has been received in foreground
*/
this.notificationListener = 
 firebase.notifications().onNotification((notification) => {
   const { title, body } = notification;
  this.showAlert(title, body);
});

/*
 If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
 */
 this.notificationOpenedListener = 
firebase.notifications().onNotificationOpened((notificationOpen) => {
   const { title, body } = notificationOpen.notification;
    this.showAlert(title, body);
 });

 /*
 If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
 */
const notificationOpen = await 
firebase.notifications().getInitialNotification();
 if (notificationOpen) {
   const { title, body } = notificationOpen.notification;
   this.showAlert(title, body);
}

}
于 2019-10-18T03:58:24.053 回答
0
"message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

通知有效负载必须是这样的。

https://firebase.google.com/docs/cloud-messaging/concept-options

于 2020-11-05T17:18:13.073 回答