I have searched AWS documentation and wasted couple of hours but could not find the API and code to send push notification using Node JS. Can anybody help in sending AWS SNS push notification using Node JS on Android and iOS devices?
Asked
Active
Viewed 1.1k times
3
-
http://docs.aws.amazon.com/sns/latest/dg/SNSMobilePushBaiduAPI.html – Adiii Jan 18 '17 at 05:48
-
1http://stackoverflow.com/questions/21609121/anyone-using-node-js-with-amazon-sns-and-apple-push-notifications – Adiii Jan 18 '17 at 05:49
2 Answers
8
Got the answer.
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: '{AWS_KEY}',
secretAccessKey: '{AWS_SECRET}',
region: '{SNS_REGION}'
});
var sns = new AWS.SNS();
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, // Required
MessageStructure: 'json',
TargetArn: {{TargetArn}} // Required
}, function(err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('push sent');
console.log(data);
});
});

Joe
- 141
- 1
- 2
- 8
2
Use sns-mobile
The below example creates an SNS instance for an Android application identified by a PlatformApplicationArn
var SNS = require('sns-mobile'),
EVENTS = SNS.EVENTS;
var SNS_KEY_ID = process.env['SNS_KEY_ID'],
SNS_ACCESS_KEY = process.env['SNS_ACCESS_KEY'],
ANDROID_ARN = process.env['SNS_ANDROID_ARN'];
var androidApp = new SNS({
platform: SNS.SUPPORTED_PLATFORMS.ANDROID,
region: 'eu-west-1',
apiVersion: '2010-03-31',
accessKeyId: SNS_ACCESS_KEY,
secretAccessKey: SNS_KEY_ID,
platformApplicationArn: ANDROID_ARN,
//sandbox: true (This is required for targetting (iOS) APNS_SANDBOX only)
});
// Add a user, the endpointArn is their unique id
// endpointArn is required to send messages to the device
androidApp.addUser('some_fake_deviceid_that_i_made_up', JSON.stringify({
some: 'extra data'
}), function(err, endpointArn) {
if(err) {
throw err;
}
// Send a simple String or data to the client
androidApp.sendMessage(enpointArn, 'Hi There!', function(err, messageId) {
if(err) {
throw err;
}
console.log('Message sent, ID was: ' + messageId);
});
});

Adiii
- 54,482
- 7
- 145
- 148