SNS - NodeJS Push Notification
by Roydon DSouza
JavaScript
//Under development - Might break
//Forming a JSON to send over SNS
// pn would be my data model. Since I haven't finalised it I have moved it with an 'OR' ||
var apnsKey = 'APNS';
var parameters= {
'image_url':'http://',
'main_order_id': '180',
'amount': 87.98,
'message': 'New booking of Rs 200 from nestee.',
'description':'Full description if any'
};
var payload = {
[apnsKey]: {
aps: {
alert: 'Notification Title' || pn.title,
sound: 'default',
badge: 1
},
"data": {
'notification_details': {
'type': 'Booking' || pn.type,
'is_show_notification': true || pn.is_show_notification,
'notification_for': 1 || pn.notification_for
},
'parameters': parameters || JSON.stringify(pn.body)
}
},
GCM: {
notification: {
'title': 'Notification Title' || pn.title,
'body': 'Notification Body' || pn.message,
'icon': 'd' || pn.icon_url
},
data: { //body
'notification_details': {
'type': 'Booking' || pn.type,
'is_show_notification': true || pn.is_show_notification,
'notification_for': 1 || pn.notification_for
},
'parameters': parameters || JSON.stringify(pn.body)
}
}
};
payload[apnsKey] = JSON.stringify(payload[apnsKey]);
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
console.log(payload);
console.log(JSON.parse(payload));
//publish
var sns = new AWS.SNS();
var endpointArn = "ARN of device paired with platform app on SNS"
/*
sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function (err, data) {
if (err) {
...