Skip to content

Instantly share code, notes, and snippets.

@NguyenTungs
Forked from tatsuyaueda/app.js
Created December 22, 2017 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NguyenTungs/0a4d00bfca8c550deb9dbf122eb98f98 to your computer and use it in GitHub Desktop.
Save NguyenTungs/0a4d00bfca8c550deb9dbf122eb98f98 to your computer and use it in GitHub Desktop.
Lambda to AWS SNS Linphone Push Notification
console.log('Loading function');
var aws = require('aws-sdk');
var sns = new aws.SNS({
apiVersion: '2010-03-31',
region: 'us-east-1'
});
exports.handler = function(params, context) {
console.log('Received event:', JSON.stringify(params, null, 2));
var endpointArn = null;
var DeviceToken = params.deviceToken;
if(!DeviceToken){
context.fail("{result:'error', message:'Device Token is empty'}");
}
sns.createPlatformEndpoint({
PlatformApplicationArn: 'PLATFORMAPPLICATIONARN',
Token: DeviceToken
}, function(err, data) {
if (err) {
console.log("createPlatformEndpoint Error");
console.log(err.stack);
regex = err.stack.match(/arn:aws:sns[^ ]+/);
if(regex.length == 1){
endpointArn = regex[0];
}
}else{
console.log("createPlatformEndpoint OK");
endpointArn = data.EndpointArn;
console.log(data);
}
if(endpointArn === null){
console.log("Can't get Platform Endpoint");
context.fail("{result:'error', message:'Can't get Platform Endpoint'}");
return;
}
console.log("ENDPOINTARN:" + endpointArn);
sns.getEndpointAttributes({
EndpointArn: endpointArn
}, function(err, data) {
if (err){
console.log("getEndpointAttributes Error");
console.log(err, err.stack);
context.fail("{result:'error', message:'Can't get Platform Endpoint Attributes'}");
return;
}else{
console.log("getEndpointAttributes OK");
console.log(data);
if(data.Attributes.Enabled != 'true' || data.Attributes.Token != DeviceToken){
console.log("UPDATE REQUEST");
}
}
//
console.log("Push");
var callerid = params.callerId;
var payload = {
'call-id': callerid,
APNS: {
aps: {
alert: {
'loc-key': 'IC_MSG',
'loc-args': [callerid]
},
sound: 'notes_of_the_optimistic.caf',
badge: 1
}
}
};
payload.APNS = JSON.stringify(payload.APNS);
payload = JSON.stringify(payload);
sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function(err, data){
if(err){
console.log("Push Fail!");
console.log(err.stack);
context.fail("{result:'error', message:'Can't get Platform Endpoint Attributes'}");
}else{
console.log("Push OK!");
console.log(data);
context.succeed("{result:'succeed', message:'Push Notification succeed.'}");
}
});
console.log('push end');
});
});
};
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:CreatePlatformEndpoint",
"sns:GetEndpointAttributes",
"sns:Publish"
],
"Resource": [
"PLATFORMAPPLICATIONARN"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment