Firebase functions for push notification - "Error: Exactly one of topic, token or condition is required"
Firebase functions for push notification - "Error: Exactly one of topic, token or condition is required"
我正在尝试使用 Firebase 上的云功能发送推送通知。最终目标是在每次有人写入数据库中的特定位置时向所有用户推送通知 /model_outputs/real_estate
。我已经确认当我在数据库中手动创建一个节点时该功能会激活。尽管如此,当它运行时,我看到了这个错误:
Error: Exactly one of topic, token or condition is required
at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:254:16)
at Object.validateMessage (/srv/node_modules/firebase-admin/lib/messaging/messaging-types.js:46:15)
at Messaging.send (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:216:27)
at children.map.child (/srv/index.js:59:37)
at Array.map (<anonymous>)
at admin.database.ref.once.then.then.children (/srv/index.js:50:24)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
errorInfo:
{ code: 'messaging/invalid-payload',
message: 'Exactly one of topic, token or condition is required' },
codePrefix: 'messaging'
这是我的函数:
exports.sendNoti =
functions.database.ref('/model_outputs/real_estate')
.onWrite((change, context) => {
return admin.database().ref("/fcmTokens").once('value')
.then(snap => {
let children = [];
snap.forEach(child_snap => {
children.push(child_snap.val());
console.log('get token'); // build children
});
return children;
})
.then(children => {
children.map(child => {
let message = {
notification: {
title: "test",
body: "body"
},
token: child.device_token
}
admin.messaging().send(message).catch(console.log);
});
return null;
})
.then( () => {
return notif.ref.remove(); // consume send request
})
.catch(console.log);
});
不得不承认这个功能不是我写的,如果能改进一下,我将不胜感激。我的 angular 和 javascript 知识相当基础。
您可以看到我正在尝试将我的用户令牌传递到一个数组中,然后向所有这些用户发送一条消息。令牌存储在我的数据库中的 /fcmTokens
中:
考虑到消息错误,似乎 child.device_token
变量中的值设置不正确,并且在没有值的情况下发送。我建议您在发送值并确认之前打印它。
完成上述操作并确认存在问题后,只需更改 token
中发送值的方式,以便您可以在消息中发送正确的值。
我正在尝试使用 Firebase 上的云功能发送推送通知。最终目标是在每次有人写入数据库中的特定位置时向所有用户推送通知 /model_outputs/real_estate
。我已经确认当我在数据库中手动创建一个节点时该功能会激活。尽管如此,当它运行时,我看到了这个错误:
Error: Exactly one of topic, token or condition is required
at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:254:16)
at Object.validateMessage (/srv/node_modules/firebase-admin/lib/messaging/messaging-types.js:46:15)
at Messaging.send (/srv/node_modules/firebase-admin/lib/messaging/messaging.js:216:27)
at children.map.child (/srv/index.js:59:37)
at Array.map (<anonymous>)
at admin.database.ref.once.then.then.children (/srv/index.js:50:24)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
errorInfo:
{ code: 'messaging/invalid-payload',
message: 'Exactly one of topic, token or condition is required' },
codePrefix: 'messaging'
这是我的函数:
exports.sendNoti =
functions.database.ref('/model_outputs/real_estate')
.onWrite((change, context) => {
return admin.database().ref("/fcmTokens").once('value')
.then(snap => {
let children = [];
snap.forEach(child_snap => {
children.push(child_snap.val());
console.log('get token'); // build children
});
return children;
})
.then(children => {
children.map(child => {
let message = {
notification: {
title: "test",
body: "body"
},
token: child.device_token
}
admin.messaging().send(message).catch(console.log);
});
return null;
})
.then( () => {
return notif.ref.remove(); // consume send request
})
.catch(console.log);
});
不得不承认这个功能不是我写的,如果能改进一下,我将不胜感激。我的 angular 和 javascript 知识相当基础。
您可以看到我正在尝试将我的用户令牌传递到一个数组中,然后向所有这些用户发送一条消息。令牌存储在我的数据库中的 /fcmTokens
中:
考虑到消息错误,似乎 child.device_token
变量中的值设置不正确,并且在没有值的情况下发送。我建议您在发送值并确认之前打印它。
完成上述操作并确认存在问题后,只需更改 token
中发送值的方式,以便您可以在消息中发送正确的值。