我正在使用Firebase FCM向我的App上的用户发送通知。我可以很好地发送通知,但是当尝试在回调函数中访问通知的数据时,没有任何反应...
这是我的代码:
//Firebase cloud function RTDB Trigger:
exports.cannectionPostNotification = functions.database.ref('/feed/{userId}/{postId}')
.onCreate((snapshot, context) => {
const userId = context.params.userId;
const post = snapshot.val();
const ref = admin.database().ref(`users/${userId}/notificationTokens`);
return ref.once("value", function(snap){
const payload = {
notification: {
title: `New Post from ${post.username}`,
body: post.capText,
},
data: {
//need to access this to navigate to
//this screen when the notification is open...
screen: 'imageScreen'
},
};
admin.messaging().sendToDevice(snap.val(), payload)
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
});
//App.js callback..
this.notificationOpenedListener = fire.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
const { screen } = notificationOpen.data;
this.showAlert(title, body);
//alert won't show when app opens from notifiation...
alert('NOTIFICATION OPENED' + screen);
});
我的目标是根据有效负载中的数据导航到适当的屏幕,但是我无法从回调中获取任何信息。
我非常感谢我提供的有关此问题的所有指导,
谢谢!