如何使用 nodejs 发送 firebase 通知?
How to send firebase notifications using nodejs?
我想自动实现推送通知并且我使用了 javascript (node.js) 但我收到了这个错误
Function returned undefined, expected Promise or value
我不是 node js 开发者我是 flutter 开发者,我不知道什么是承诺。
这是我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var notificationMessageData;
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot , context) => {
notificationMessageData = snapshot.data();
admin.firestore().collection('pushTokens').get().then(async (snapshot) => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return await admin.messaging().sendToDevice(tokens , payload).then((response) => {
console.log('nice');
}).catch((err) => {
console.log(err);
})
}
})
})
一切正常,我上传它没有任何问题,但是在将文档添加到帖子集合时,它在日志中输出了上述错误。
我已经创建了一个用户注册表单,我已经注册了用户并将他们的令牌 ID 放在一个名为 pushTokens 的集合中,然后为该集合中的每个用户发送一个通知,但这没有用。
它说:
Function returned undefined, expected Promise or value
您是否尝试过从函数中 returning null
、true
或 false
等?
Promise 是您在程序中传递的东西,同时等待它变成一个真正的值。
似乎错误消息的重点是要您 return 从您的函数中执行某些操作。在不知道更多的情况下,这是我能给的最好的建议。
你的代码有两个问题:
- 您没有 return 异步 Firebase 方法(
get()
和 sendToDevice()
)return 的承诺;
- 您混淆了
async/await
与 then()
方法的使用。
我建议您观看 Firebase video series, and then that you first try with using the then()
method to correctly chain 您的 Promises 和 return 链中关于 "JavaScript Promises" 的 3 个官方视频。
下面的代码应该可以工作。
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
return admin.firestore().collection('pushTokens').get()
.then(snapshot => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
throw new Error('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return admin.messaging().sendToDevice(tokens, payload)
}
})
.catch((err) => {
console.log(err);
return null;
})
});
那么,在体验了then()
(和catch()
)异步方法的"management"之后,你可以尝试一下async/await
:再次,这个official video 来自 Doug Stevenson 的文章会有很大帮助。
我想自动实现推送通知并且我使用了 javascript (node.js) 但我收到了这个错误
Function returned undefined, expected Promise or value
我不是 node js 开发者我是 flutter 开发者,我不知道什么是承诺。
这是我的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var notificationMessageData;
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot , context) => {
notificationMessageData = snapshot.data();
admin.firestore().collection('pushTokens').get().then(async (snapshot) => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return await admin.messaging().sendToDevice(tokens , payload).then((response) => {
console.log('nice');
}).catch((err) => {
console.log(err);
})
}
})
})
一切正常,我上传它没有任何问题,但是在将文档添加到帖子集合时,它在日志中输出了上述错误。
我已经创建了一个用户注册表单,我已经注册了用户并将他们的令牌 ID 放在一个名为 pushTokens 的集合中,然后为该集合中的每个用户发送一个通知,但这没有用。
它说:
Function returned undefined, expected Promise or value
您是否尝试过从函数中 returning null
、true
或 false
等?
Promise 是您在程序中传递的东西,同时等待它变成一个真正的值。
似乎错误消息的重点是要您 return 从您的函数中执行某些操作。在不知道更多的情况下,这是我能给的最好的建议。
你的代码有两个问题:
- 您没有 return 异步 Firebase 方法(
get()
和sendToDevice()
)return 的承诺; - 您混淆了
async/await
与then()
方法的使用。
我建议您观看 Firebase video series, and then that you first try with using the then()
method to correctly chain 您的 Promises 和 return 链中关于 "JavaScript Promises" 的 3 个官方视频。
下面的代码应该可以工作。
exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
const notificationMessageData = snapshot.data();
return admin.firestore().collection('pushTokens').get()
.then(snapshot => {
var tokens = [];
if (snapshot.empty) {
console.log('No Devices');
throw new Error('No Devices');
} else {
for (var token of snapshot.docs) {
tokens.push(token.data().tokenID);
}
var payload = {
"notification": {
"title": "from" + notificationMessageData.writer,
"body": "from" + notificationMessageData.name,
"sound": "default"
},
"data": {
"sendername": notificationMessageData.writer,
"message": notificationMessageData.name
}
}
return admin.messaging().sendToDevice(tokens, payload)
}
})
.catch((err) => {
console.log(err);
return null;
})
});
那么,在体验了then()
(和catch()
)异步方法的"management"之后,你可以尝试一下async/await
:再次,这个official video 来自 Doug Stevenson 的文章会有很大帮助。