I found an odd behavior of AWS SNS when publishing multiple messages to Apple Push Notifications at the same time from AWS Lambda - every published message is received TWICE by the subscriber.
My code is as follows
handler.js
const uuid = require('uuid')
const aws = require('aws-sdk');
const sns = new aws.SNS({ apiVersion: '2010-03-31' })
const messageSender = async (event, context, callback) => {
// sending 3 messages one after another
publishMessage("title1", "message1")
publishMessage("title2", "message2")
publishMessage("title3", "message3")
return []
}
const publishMessage = (title, message) => {
sns.publish({
Message: JSON.stringify({
"default": "default message",
"APNS_SANDBOX": JSON.stringify({
"aps": {
"alert": {
"title": title,
"body": message
},
"badge": 1,
"id": uuid.v4(),
"foo": "bar"
}
})
}),
MessageStructure: 'json',
TopicArn: process.env.TOPIC_ARN
}).promise()
console.log(`Message for topic is published: ${message}`)
}
However if I publish only one message like the below, the exact message is received only once.
const messageSender = async (event, context, callback) => {
publishMessage("title1", "message1")
return []
}
Any ideas why receiving the same message twice when sending multiple messages?
EDIT
After playing the publish API a while I found the following.
The duplication is caused by a potential Amazon SNS bug(?). If not sending JSON format the duplication error disappears. e.g. Remove the MessageStructure: 'json'
and change the message to be String only as follows.
sns.publish({
Message: "this is a sample message",
TopicArn: process.env.TOPIC_ARN
}).promise()
This is a workaround of the issue however the underlying cause of the original issue is still unknown.
The workaround has drawbacks as it cannot customise the notification with APN attributes such as adding a title to the push notifications as well as badges.
Any other workaround or whoever knows the fixes?
After spending hours scratching my head I finally found the issue.
I added a customised id
field in the json message which causes the same message being sent twice when publishing multiple messages same time.
After removed the id
field, the messages are only sent once no matter how many messages are published at the same time.
{
"default": "default message",
"APNS_SANDBOX": JSON.stringify({
"aps": {
"alert": {
"title": title,
"body": message
},
"badge": 1,
// "id": uuid.v4(), <---- THIS LINE IS REMOVED
"foo": "bar"
}
})
}
ps. I also noticed if the id
remains, when publishing messages at an interval of more than 3 seconds, the same message is delivered once.