【问题标题】:Send a push notification with external link (React Native)发送带有外部链接的推送通知(React Native)
【发布时间】:2019-10-18 03:22:37
【问题描述】:

我使用 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);
 })

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

【问题讨论】:

    标签: firebase react-native push-notification react-native-firebase react-native-fcm


    【解决方案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);
    }
    
    }
    

    【讨论】:

    【解决方案2】:
    "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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多