我只是想让我的应用程序在有人喜欢那里的事件时向用户发送通知。
我试图这样做:
exports.likeAddedNotification = functions.database.ref('/eventLikes/{eventID}/{userWhoLikedID}').onWrite(event => {
const userWhoLikedID = event.params.userWhoLikedID;
console.log("User has liked a post " + userWhoLikedID);
admin.database().ref('events/' + event.params.eventID).once('value').then(function(snapshot) {
var usersID = snapshot.val().userId;
console.log("The person who made the post is " + usersID);
//Send notification to that user
var payload = {
"group_id": "batch_push_sender",
"push_time": "now",
"message": {
"body": usersID + " liked your event"
},
"recipients": {
"custom_ids": [usersID]
},
"sandbox": true, // Only for iOS
};
notificationRequest.write(JSON.stringify(payload));
notificationRequest.end();
console.log("Sent a notification to " + usersID);
});
});
(很抱歉包含这么多代码,但我觉得这可能都是相关的)如果我使用此代码并删除
var payload = ...
和
notificationRequest.write(JSON.stringify(payload));
notificationRequest.end();
它完全符合我的预期。它将写入控制台,说明喜欢哪个帖子以及喜欢该帖子的人。
我真的不确定问题是否来自于在闭包中做了很多事情,或者我只是在做一些明显错误的事情,或者是否是导致错误的 Batch API。
如果您知道一个更好的推送通知站点,它允许使用 UDID 作为自定义标识符,这样我就不必手动处理每个设备的令牌,请告诉我!