-
-
Save NguyenTungs/0a4d00bfca8c550deb9dbf122eb98f98 to your computer and use it in GitHub Desktop.
Lambda to AWS SNS Linphone Push Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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