I have configured my push notifications using the node apn library.
I use an AWS lambda function to send the notification and it is called from another lambda function using event as the invocation type as follows:
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);
}
});
and the send notification lambda function:
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);
}
});
}
Sometimes the notifications get out of sync and when I send a message which sends it's push notification, the notification from the previous message is received. Not sure why this is happening or how to fix it.