Skip to content

Instantly share code, notes, and snippets.

@tmarshall
Last active October 30, 2022 06:12
Show Gist options
  • Save tmarshall/6149ed2475f964cda3f5 to your computer and use it in GitHub Desktop.
Save tmarshall/6149ed2475f964cda3f5 to your computer and use it in GitHub Desktop.
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);
});
});
@lucasrleandro
Copy link

lucasrleandro commented Jan 8, 2020 via email

@lucasrleandro
Copy link

lucasrleandro commented Jan 8, 2020 via email

@MichaelHuy
Copy link

Sure. iOS works well.

@MichaelHuy
Copy link

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

@MichaelHuy
Copy link

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
Copy link

lucasrleandro commented Jan 8, 2020 via email

@MichaelHuy
Copy link

MichaelHuy commented Jan 8, 2020

Thanks @lucasrleandro. Good night

@lucasrleandro
Copy link

lucasrleandro commented Jan 8, 2020 via email

@MichaelHuy
Copy link

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

@lucasrleandro
Copy link

lucasrleandro commented Jan 9, 2020 via email

@sankar9659
Copy link

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

@pdkproitf
Copy link

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

@iammateus
Copy link

Very helpful, thank you

@scastrec
Copy link

Looks like there is some stuff to add.
The platformEndpointArn can be created but inactive.
I had a lot of trouble with it and currently working on it.
AWS doc

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