通知未通过火力基础函数发送。 "undefined"已登录控制台



我正在尝试根据令牌向所有设备发送示例通知,但是令牌被记录为"未定义",通知随后无法传递

我的代码中的以下行成功地向我展示了数据库中的数据:

const notificationSnapshot = change.after.val(); //get new value
console.info(notificationSnapshot);

但是,尽管上述数据成功检索,但以下内容给出了"未定义"。

const userToken = notificationSnapshot.token;
console.info(userToken);

这难道不是检索令牌以将通知发送到 Firebase 数据库中的所有已注册设备的正确方法吗?

我的整个函数(索引.js(

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendSampleNotification = functions.database.ref('/User')
.onWrite((change, context) => {
const notificationSnapshot = change.after.val(); //get new value
const userToken = notificationSnapshot.token;
const name = notificationSnapshot.name;
const surname = notificationSnapshot.surname;
console.info(notificationSnapshot);
console.info(userToken);
var message = {
notification: {
title: 'test title',
body: 'test message'
},
token: userToken
};
admin.messaging().send(message).then((response) => {
console.log("Message sent successfully:", response);
return response;
})
.catch((error) => {
console.log("Error sending message: ", error);
});
});

我想说你的问题与此非常相似,因为由于执行时间,你有一个丢失的令牌(显示为未定义(,或多或少是Doug指出的。

请注意,该解决方案依赖于考虑执行时间,我也看到某些方法执行中的实现不同,但我会说将军指向同一方向。

相关内容

最新更新