Badge count option in amazon SNS

烂漫一生 提交于 2019-12-10 16:26:38

问题


I need to get badge count in my application. I am using amazon SNS service. Here is my code

AWS.config.update({
  accessKeyId: 'XXXXXXX',
  secretAccessKey: 'XXXXXXX'
})
AWS.config.update({ region: 'XXXXXXX' })
const sns = new AWS.SNS()
const params = {
  PlatformApplicationArn: 'XXXXXXX',
  Token: deviceToken
}
sns.createPlatformEndpoint(params, (err, EndPointResult) => {
  const client_arn = EndPointResult["EndpointArn"];
    sns.publish({
      TargetArn: client_arn,
      Message: message,
      Subject: title,
      // badge: 1
    }, (err, data) => {
    })
  })

I need to know where I can add badge option here?

Thank you!!!


回答1:


We can send json message to AWS SNS to send push notification to application endpoint. Which allows us to send platform (APNS, FCM etc) specific fields and customizations.

Example json message for APNS is,

{
"aps": {
    "alert": {
        "body": "The text of the alert message"
    },
    "badge": 1,
    "sound": "default"
 }
}

This is how you can send request,

   var sns = new AWS.SNS();
   var payload = {
      default: 'This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for 
  one of the notification platforms.',
      APNS: {
        aps: {
          alert: 'The text of the alert message',
          badge: 1,
          sound: 'default'
        }
      }
    };

    // stringify inner json object
    payload.APNS = JSON.stringify(payload.APNS);
    // then stringify the entire message payload
    payload = JSON.stringify(payload);

    sns.publish({
      Message: payload,      // This is Required
      MessageStructure: 'json',
      TargetArn: {{TargetArn}} // This Required
    }, function(err, data) {
      if (err) {
        console.log(err.stack);
        return;
      }
    });
  });

If you need to support multiple platforms then as per AWS docs,

To send a message to an app installed on devices for multiple platforms, such as FCM and APNS, you must first subscribe the mobile endpoints to a topic in Amazon SNS and then publish the message to the topic.

APNS specific payload keys can be found APNS Payload Key Reference.

AWS SNS documentation can be found here Send Custom, Platform-Specific Payloads to Mobile Devices



来源:https://stackoverflow.com/questions/57691028/badge-count-option-in-amazon-sns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!