从AWS Lambda Node.js向Android手机发送AWS SNS通知

时间:2017-07-22 17:51:39

标签: android node.js amazon-web-services aws-lambda amazon-sns

我想将AWS SNS从ASW lambda node.js发送到Android设备端点,但我一直有这个错误: “com.amazonaws.mobileconnectors.lamdainvoker.LambdaFunctionException:未处理的”

代码基本上类似于我在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);

    }
});
});

lambda函数策略设置为;在Android应用程序ARN上“效果”:“允许”“sns:*”。 所以我猜这不是角色政策问题。 任何帮助表示赞赏。 感谢。

1 个答案:

答案 0 :(得分:2)

向Android端点发送通知:

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);
 });
});

根据自己的情况进行修改。