Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
aws-sdk sns example, in Node.js
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: '{AWS_KEY}',
secretAccessKey: '{AWS_SECRET}',
region: '{SNS_REGION}'
});
var sns = new AWS.SNS();
sns.createPlatformEndpoint({
PlatformApplicationArn: '{APPLICATION_ARN}',
Token: '{DEVICE_TOKEN}'
}, function(err, data) {
if (err) {
console.log(err.stack);
return;
}
var endpointArn = data.EndpointArn;
var payload = {
default: 'Hello World',
APNS: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
}
};
// first have to stringify the inner APNS object...
payload.APNS = JSON.stringify(payload.APNS);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
console.log('sending push');
sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function(err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('push sent');
console.log(data);
});
});
@MrMotski

This comment has been minimized.

Copy link

@MrMotski MrMotski commented Dec 29, 2015

Very helpful for understanding how to interface with SNS!

@tony-gutierrez

This comment has been minimized.

Copy link

@tony-gutierrez tony-gutierrez commented Feb 9, 2016

Thanks

@faisalirfan361

This comment has been minimized.

Copy link

@faisalirfan361 faisalirfan361 commented Feb 17, 2016

great job Man :)

@nicholasf

This comment has been minimized.

Copy link

@nicholasf nicholasf commented Apr 11, 2016

Nice share.

@benag

This comment has been minimized.

Copy link

@benag benag commented Jul 5, 2016

how do you update the token?

@xavier-musy

This comment has been minimized.

Copy link

@xavier-musy xavier-musy commented Sep 29, 2016

Thanks for sharing this

@robomotic

This comment has been minimized.

Copy link

@robomotic robomotic commented Oct 19, 2016

I have tried to send silent notifications by using your code:

      var payload = {
        default: "jcard",
        APNS: {
          aps: {
            "content-available": 1
          },
          "jcard":"SILENT"
        }
      };

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

But they keep appearing with the default text jcard on my mobile.
What am I doing wrong?

@hopewise

This comment has been minimized.

Copy link

@hopewise hopewise commented Dec 26, 2016

'{AWS_KEY}' should be changed to process.env.AWS_KEY , I think

@prahaladm

This comment has been minimized.

Copy link

@prahaladm prahaladm commented Jan 20, 2017

Thanks for sharing.
For sending Voip pushes use:
var payload = {
default: 'Hello World',
APNS_VOIP: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
}
};

@joonas-fi

This comment has been minimized.

Copy link

@joonas-fi joonas-fi commented Feb 4, 2017

Thanks for sharing!

@sw-tt-jigneshvagh

This comment has been minimized.

Copy link

@sw-tt-jigneshvagh sw-tt-jigneshvagh commented Mar 9, 2017

This is for only for iOS ?

@constantx

This comment has been minimized.

Copy link

@constantx constantx commented Mar 13, 2017

❤️

@X-Strange

This comment has been minimized.

Copy link

@X-Strange X-Strange commented Apr 22, 2017

@mansuu

This comment has been minimized.

Copy link

@mansuu mansuu commented May 18, 2017

but how to setup web app

@ehorodyski

This comment has been minimized.

Copy link

@ehorodyski ehorodyski commented May 23, 2017

Anything on how to set up MessageAttributes? I need to hook TTL in but have no idea how it would be supported...I guess a JSON that's stringified?

@Bratkartoffl

This comment has been minimized.

Copy link

@Bratkartoffl Bratkartoffl commented Aug 10, 2017

@robomotic did you get this running?
@X-Strange your links returns a 404. Could you please post your solution again?

@Bratkartoffl

This comment has been minimized.

Copy link

@Bratkartoffl Bratkartoffl commented Aug 10, 2017

Found a Solution. Just use brackets.

"content-available" : 1

@thenb

This comment has been minimized.

Copy link

@thenb thenb commented Sep 3, 2017

perfect!! works like a charm!!
thanks dude

@NguyenTungs

This comment has been minimized.

Copy link

@NguyenTungs NguyenTungs commented Dec 22, 2017

Thanks @tmarshall.
I think, This is for iOS device, do u? So android, how can build send notification for android. Please!

@saurabhschauhan

This comment has been minimized.

Copy link

@saurabhschauhan saurabhschauhan commented Mar 5, 2018

How can I add my additional data to the notification?

@jitendra-kr

This comment has been minimized.

Copy link

@jitendra-kr jitendra-kr commented Oct 10, 2018

Hi, i am used this working fine. Is there a way to send additional data into payload?

@Sojer23

This comment has been minimized.

Copy link

@Sojer23 Sojer23 commented Apr 17, 2019

Thanks!

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jul 30, 2019

Thanks man!!!!!!!!!
Helped a lot!!!!
The AWS Documentation was not clear.

@mahees

This comment has been minimized.

Copy link

@mahees mahees commented Dec 20, 2019

thank you!!! i kept doing TopicARN vs TargetArn sigh.

@malikid

This comment has been minimized.

Copy link

@malikid malikid commented Dec 24, 2019

Code for Android if you're using FCM:

var payload = {
    default: 'Hello World',
    GCM: {
        notification: {
            title: 'Hello World',
            body: 'Notification Body'
            // other configs can be put here
        }
    },
    // APNS: {
    //  aps: {
    //    alert: 'Hello World',
    //    sound: 'default',
    //    badge: 1
    //  }
    // }
  };

  // first have to stringify the inner GCM / APNS object...
  payload.GCM = JSON.stringify(payload.GCM);
  // payload.APNS = JSON.stringify(payload.APNS);
  // then have to stringify the entire message payload
  payload = JSON.stringify(payload);
@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Hi pro,
I am facing an Android app with react native using react-native-push-notification. I can use sns console to trigger sending push Notification to Android device
{ "GCM": "{ \"data\": { \"message\": \"Sample message for Android endpoints\" } }" }
But when using sns publish with the code above, I did not receive Push Notification on my device...
Any advice?
Thank you.

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Try using the payload like this: var payload = { default: "something", GCM: { notification: { message: "E", payload: "F", title: "title", body: "body" } } }; payload.GCM = JSON.stringify(payload.GCM); payload = JSON.stringify(payload); Em ter, 7 de jan de 2020 23:52, Huy Le notifications@github.com escreveu:

Hi pro, I am facing on Android app with react native using react-native-push-notification. I can use sns console to trigger sending push Notification to Android device { "GCM": "{ "data": { "message": "Sample message for Android endpoints" } }" } But when using sns publish with the code above, I did not receive Push Notification on my device... Any advice? Thank you. — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/6149ed2475f964cda3f5?email_source=notifications&email_token=AKDCZFIHPPNCTU3ZQKDH4ALQ4U5YFA5CNFSM4IHYIJGKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF7CTC#gistcomment-3130673, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKDCZFJSRQ36WLQZFOVL3JLQ4U5YFANCNFSM4IHYIJGA .

Thanks for your reply. I tried with your payload but still not receive a push notification to my android device.

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Via SNS console the push is received? Em qua, 8 de jan de 2020 00:18, Huy Le notifications@github.com escreveu:

@lucas https://github.com/lucas Try using the payload like this: var payload = { default: "something", GCM: { notification: { message: "E", payload: "F", title: "title", body: "body" } } }; payload.GCM = JSON.stringify(payload.GCM); payload = JSON.stringify(payload); Em ter, 7 de jan de 2020 23:52, Huy Le @.*** escreveu: … <#m_7011924555103773334_> Hi pro, I am facing on Android app with react native using react-native-push-notification. I can use sns console to trigger sending push Notification to Android device { "GCM": "{ "data": { "message": "Sample message for Android endpoints" } }" } But when using sns publish with the code above, I did not receive Push Notification on my device... Any advice? Thank you. — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/6149ed2475f964cda3f5?email_source=notifications&email_token=AKDCZFIHPPNCTU3ZQKDH4ALQ4U5YFA5CNFSM4IHYIJGKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF7CTC#gistcomment-3130673, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKDCZFJSRQ36WLQZFOVL3JLQ4U5YFANCNFSM4IHYIJGA . Thanks for your reply. I tried with your payload but still not receive a push notification to my android device. — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/6149ed2475f964cda3f5?email_source=notifications&email_token=AKDCZFKBTNR4LUADUBRYTE3Q4VAZRA5CNFSM4IHYIJGKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF7CUE#gistcomment-3130690, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKDCZFNNORXQFJZEYYQQQS3Q4VAZRANCNFSM4IHYIJGA .

Yes. Very weird.

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Sure. iOS works well.

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

image
sent push successfully, but not show anything on my device.

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Oh... Really I received a push notification from code trigger. But too long. maybe random. :( . What happened with Android push with amazon sns

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 8, 2020

Thanks @lucasrleandro. Good night

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 8, 2020

@MichaelHuy

This comment has been minimized.

Copy link

@MichaelHuy MichaelHuy commented Jan 9, 2020

Yes, Lucas. It is working now. Root cause: my android app crash then it will not receive Push Notification.
Thanks

@lucasrleandro

This comment has been minimized.

Copy link

@lucasrleandro lucasrleandro commented Jan 9, 2020

@sankar9659

This comment has been minimized.

Copy link

@sankar9659 sankar9659 commented Feb 19, 2020

getting this error 'User: arn:aws:iam::***********:user/******* is not authorized to perform: SNS:Publish on resource:'

@pdkproitf

This comment has been minimized.

Copy link

@pdkproitf pdkproitf commented Feb 25, 2020

MessageStructure seems does not work.
When I add MessageStructure to publish parameters, it does not send a notification to the subscriber.

@iammateus

This comment has been minimized.

Copy link

@iammateus iammateus commented Mar 8, 2021

Very helpful, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment