我正在使用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()发送的标题和正文相同。
任何帮助将不胜感激。谢谢。