0

我只是想让我的应用程序在有人喜欢那里的事件时向用户发送通知。

我试图这样做:

    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 作为自定义标识符,这样我就不必手动处理每个设备的令牌,请告诉我!

4

1 回答 1

1

首先,确保返回您的 Promise,这是保证 Cloud Functions 工作人员继续进行的唯一方法。由于 Firebase 云通知是同步的,因此您的请求很可能被中止。

您还应该查看 Firebase Admin SDK 的消息传递组件。我们为 FCM 构建了一些不错的 API。我们在 GitHub https://github.com/firebase/functions-samples/tree/master/fcm-notifications上有一个关于如何在 Cloud Functions for Firebase 中使用这些的示例

于 2017-03-24T08:05:54.603 回答