0

我已经使用节点 apn 库配置了我的推送通知。

我使用 AWS lambda 函数来发送通知,并使用 event 作为调用类型从另一个 lambda 函数调用它,如下所示:

var pushPayload = { "users": users, "message": message };

var pushParams = {
    FunctionName: 'function-name',
    InvocationType: 'Event',
    LogType: 'Tail',
    Payload: JSON.stringify(pushPayload)
};

lambda.invoke(pushParams, function (err, data) {
    if (err) {
        callback(error);
    } else {
        callback(null, event.arguments.input);
    }
});

和发送通知 lambda 函数:

var options = {
    token: {
        key: "key",
        keyId: keyId,
        teamId: teamId
    },
    production: true
};
var message = event.message;
var users = event.users;

var apnProvider = new apn.Provider(options);

var iterationComplete = false;

for (var j = 0; j < users.length; j++) {
    if (j === (users.length - 1)) {
        iterationComplete = true;
    }

    var deviceToken = users[j].user_device_token;

    var notification = new apn.Notification();

    notification.alert = message;

    notification.contentAvailable = 1;

    notification.topic = "com.example.Example";

    context.callbackWaitsForEmptyEventLoop = false;
    apnProvider.send(notification, [deviceToken]).then((response) => {
        if (iterationComplete) {
            context.succeed(event);
        }
    });
}

有时通知会不同步,当我发送一条发送它的推送通知的消息时,会收到来自上一条消息的通知。不知道为什么会发生这种情况或如何解决它。

4

0 回答 0