Last active
December 22, 2017 07:14
-
-
Save tatsuyaueda/5ec9debca26160db40ca to your computer and use it in GitHub Desktop.
Lambda to AWS SNS Linphone Push Notification
This file contains hidden or 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 hidden or 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
呼び出す側はこんな感じ
curl 'https://XXXXX.execute-api.us-east-1.amazonaws.com/prod/APINAME' --request POST
--header 'x-api-key: XXXXX'
--header 'Content-Type: application/json'
--data "{"callerId":"$2","deviceToken":"$1"}"