【发布时间】:2019-03-03 03:41:48
【问题描述】:
我正在尝试使用适用于 Android 应用程序的 Firebase 云功能向多个用户发送通知
这就是我所做的。
exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}')
.onWrite(event => {
const message = event.data.current.val();
const senderUid = message.userId;
const groupId = message.groupId;
const promises = [];
const getInstanceGroupUsers = admin.database().ref(`/groups/${groupId}/users`).once('value').then((snapshot) => {
if (snapshot.exists()) {
snapshot.forEach((child) => {
var receiverUid = child.key;
console.log(receiverUid);
if (senderUid == receiverUid) {
//if sender is receiver, don't send notification
promises.push(event.data.current.ref.remove());
return Promise.all(promises);
}
const getInstanceIdPromise = admin.database().ref(`/users/${receiverUid}/message_token`).once('value');
const getReceiverUidPromise = admin.auth().getUser(receiverUid);
return Promise.all([getInstanceIdPromise, getReceiverUidPromise]).then(results => {
const instanceId = results[0].val();
const receiver = results[1];
console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);
const payload = {
notification: {
title: receiver.displayName,
body: message.message,
icon: receiver.photoURL
}
};
admin.messaging().sendToDevice(instanceId, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
});
}
});
});
我以前从未在 JS 中编写过代码,也从未使用过云功能,但我在这里尝试做的是从节点 /notifications/messages/{pushId} 获取消息的 groupeId,然后对组的所有用户进行循环以发送通知。
我不知道我是否做得对,但这些是我得到的错误:
22:14 错误每个 then() 应该返回一个值或抛出 承诺/总是返回
28:34 错误应为 '===' 而看到的是 '==' eqeqeq
36:30 警告避免嵌套承诺
承诺/不嵌套36:94 错误 每个 then() 都应该返回一个值或抛出 承诺/总是返回
49:27 警告避免嵌套承诺
承诺/不嵌套49:27 警告避免嵌套承诺
承诺/不嵌套50:37 警告意外的函数表达式
首选箭头回调50:37 错误 每个 then() 都应该返回一个值或抛出 承诺/总是返回
53:38 警告意外的函数表达式
首选箭头回调
欢迎任何形式的帮助!
【问题讨论】:
-
错误实际上告诉您问题所在。哪些你不明白?
标签: javascript android firebase promise google-cloud-functions