Reputation: 1514
I want to send AWS SNS to an android device endpoint from ASW lambda node.js but I keep having this error: "com.amazonaws.mobileconnectors.lamdainvoker.LambdaFunctionException:Unhandled"
the code is basically similar examples I saw here on stackoverflow:
config = require("./config.js").config;
var token = "1234567898123456789";
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: config.AWSAccessKeyId, secretAccessKey:
config.AWSSecretKey});
AWS.config.update({region: config.AWSRegion});
var sns = new AWS.SNS();
var params =
{'PlatformApplicationArn':config["AWSTargetARN"],'Token':token};
var message = 'Test';
var subject = 'Stuff';
sns.createPlatformEndpoint(params,function(err,EndPointResult)
{
var client_arn = EndPointResult["EndpointArn"];
sns.publish({
TargetArn: client_arn,
Message: message,
Subject: subject},
function(err,data){
if (err)
{
console.log("Error sending a message "+err);
}
else
{
console.log("Sent message: "+data.MessageId);
}
});
});
The lambda function policy is set to; "Effect": "Allow" "sns: *" on the android applicationARN. So I'm guessing this is not a Role policy issue. Any help is appreciated. Thanks.
Upvotes: 2
Views: 1278
Reputation: 1514
Sending notification to an android endpoint:
var sns = new AWS.SNS();
AWS.config.update({
accessKeyId: theaccessKeyId,
secretAccessKey: thesecretAccessKey,
region: theregion
});
sns.createPlatformEndpoint({
PlatformApplicationArn: theapplicationARN,
Token: theToken
}, function(err, data) {
if (err) {
callback(null, JSON.stringify(err));
console.log(err.stack);
return;
}
var endpointArn = "endpoinARN of receiver";
var payload = {
"default": "The message string.",
"APNS": "{\"aps\":{\"alert\": \"Check out these awesome
deals!\",\"url\":\"www.amazon.com\"} }",
"GCM":"{\"data\":{\"message\":\"Check out these awesome
deals!\",\"url\":\"www.amazon.com\"}}",
"ADM": "{ \"data\": { \"message\": \"Check out these awesome
deals!\",\"url\":\"www.amazon.com\" }}"
};
// first have to stringify the inner GCM object...
payload.GCM = JSON.stringify(payload.GCM);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
console.log('sending push');
sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function(err, data) {
if (err) {
callback(null, JSON.stringify(err));
//console.log(err.stack);
return;
}
callback(null, "Sent message successfully");
//console.log('push sent');
// console.log(data);
});
});
Modify to your own situation.
Upvotes: 3