【发布时间】:2018-11-04 13:44:53
【问题描述】:
在我的实时数据库中创建节点时,我必须向多个令牌发送消息。
我使用那个代码,但是任何通知都丢失了(人们没有收到它)。
exports.sendMessage = functions.database.ref('/messages/{messageId}')
.onCreate((snapshot, context) => {
const original = snapshot.val();
let msg = {
message: {
data: {
title: 'title2 test',
body: 'body2 test',
notify_type: 'chat_message',
notify_id: ((new Date()).getTime()).toString(),
},
apns: {
headers: {
'apns-priority': '10',
'apns-expiration': '0'
},
payload: {
aps: { contentAvailable: true, sound:'' },
'acme1': 'bar',
title: 'title test',
body: 'body test',
notify_type: 'chat_message',
notify_id: ((new Date()).getTime()).toString()
}
},
token: token
}
};
var query = firebase.database().ref("users");
return query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var user = childSnapshot.val();
var token = user.token;
var username = user.username;
msg.message.token = token;
admin.messaging().send(msg.message).then((response) => {
console.log('message sent to '+username);
}).catch((error) => {
console.log(error);
});
});
});
});
“回报”承诺正确吗?我想我必须等待所有的“admin.messagging() Promise,但我不知道我该怎么做。
非常感谢。
【问题讨论】:
-
admin.messaging().send() 返回一个承诺。你需要注意这些。
-
最好的方法是“返回”所有的 Promise,但我该怎么做呢? Promise.all() 可以解决吗?
-
如果您不确定,可以通过此视频系列了解 Promise。 youtube.com/watch?v=7IkUgCLr5oA
标签: javascript firebase firebase-cloud-messaging google-cloud-functions firebase-admin