首页 > 解决方案 > AWS SNS always publish duplicate messages to Platform Applications Endpoint

问题描述

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?

标签: node.jsamazon-web-servicesaws-lambdaapple-push-notificationsamazon-sns

解决方案


I had the same problem, but the above didn't help.

In my case, I tracked it down to using a callback function AND the .promise() method.

i.e. I had something like:

await SNS.publish({
    TopicArn: process.env.SNS_TOPIC,
    Message: message,
    MessageAttributes: {
        source: {DataType: 'String', StringValue: source},
        level: {DataType: 'String', StringValue: level}
    },
}, function(err, data) {
    if (err) {
        console.log('SNS_notify: error -', err);
    } else {
        console.log('SNS_notify: sent -', data.MessageId);
    }
).promise();

Changing this to:

let data = await SNS.publish({
    TopicArn: process.env.SNS_TOPIC,
    Message: message,
    MessageAttributes: {
        source: {DataType: 'String', StringValue: source},
        level: {DataType: 'String', StringValue: level}
    },
}).promise();

console.log('SNS_notify: sent -', data.MessageId);

fixed the problem.


推荐阅读