4

我正在使用 react-native firebase 进行推送通知,它们工作正常。通知是正确的。我面临的问题是,当我收到通知时,通知有效负载的“标题”和“正文”将变得未定义。

调试时,我检查了要发送的正文和标题,发现这是我要发送的正确数据。

这是我发送推送通知的地方。

sendPushNotification = async (fcmToken, title, body) => {
const payload = {
  to: fcmToken,
  notification: {
    title: title,
    body: body,
    sound: "true"
  }
}

fetch("https://fcm.googleapis.com/fcm/send", {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'key=*****'
  },
  body: JSON.stringify(payload),
})
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson)
  })
  .catch((error) => {
    console.log(error);
  });

}

这就是我使用通知侦听器的地方,标题、正文和声音显示未定义。

async createNotificationListeners(nav) {
    /*
    * Triggered when a particular notification has been received in foreground
    * */
    this.notificationListener = firebase.notifications().onNotification( async (notification) => {
            const { title, body } = notification;
            await this.getConnectionsRequests();
            console.log("we are here")
            //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( async (notificationOpen) => {
            const { title, body } = notificationOpen.notification;
            nav.navigate("MyChats")
            await this.getConnectionsRequests();
            //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;
            nav.navigate("MyChats")
            // this.showAlert(title, body);
    }
    /*
    * Triggered for data only payload in foreground
    * */
    this.messageListener = firebase.messaging().onMessage((message) => {
    //process data message
    console.log(JSON.stringify(message));
    });
    } 

我在另一个屏幕中使用这些侦听器,如下所示:

componentDidMount = async () => {
    await this.props.store.createNotificationListeners(this.props.navigation);

}

当调用 notificationOpenedListener 时,标题和正文应该与我使用 sendPushNotification() 发送的相同。

任何帮助,将不胜感激。谢谢你。

4

0 回答 0