这是我发布到主题的代码(出于安全原因,我已经更改了目标和主题arn):
var AWS = require("aws-sdk");
var sns = new AWS.SNS();
var targetArn = 'arn:aws:sns:us-east-1:4363657289:endpoint/GCM/APP_NAME/3185sfdnfe283925sgSeaa0e';
var topicArn = 'arn:aws:s-s:us-east-1:4363657289436:TOPIC_NAME';
var payload = {
GCM: {
data: {
message: "test"
}
}
};
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
var params= {
TopicArn: topicArn,
TargetArn: targetArn,
Message: payload,
MessageStructure: 'json'
};
var responsefromSNS = sns.publish(params , function(error, data) {
if (error) {
console.log("ERROR: " + error.stack);
}
else {
console.log("SENT DATA: " + JSON.stringify(data));
context.done(null, data);
}
});
console.log(responsefromSNS);
我的问题是,我从未看到来自if或else块的日志语句,推送通知永远不会到达移动应用程序。我已经咨询了AWS JavaScript SDK Documentation和无数的堆栈溢出帖子,并且没有尝试过。并且,我已经将lambda函数权限发布到该主题。
--- ----- UPDATE 我已经改变了我的代码,现在它看起来像这样:
var AWS = require("aws-sdk");
AWS.config.update({region:'us-east-1'});
var topicarn = 'arn:aws:s-s:us-east-1:927579412028:alexapushdemo';
var targetarn = 'arn:aws:sns:us-east-1:927579412028:endpoint/GCM/automation.home.visa.com.homeautomation/3af761b2-1955-34d8-b66a-85e232e0aa0e';
var payload = {
default: "test",
GCM: {
data: {
message: "test"
}
}
};
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
var sns = new AWS.SNS();
console.log('start of sns function')
sns.publish({
TargetArn: targetarn,
Message: payload,
MessageStructure: 'json'
}, function(err, data) {
if (err) {
console.log(err.stack);
// Notify Lambda that we are finished, but with errors
context.done(err, 'sns function finished with errors!');
return;
}
console.log('push sent');
console.log(data);
// Notify Lambda that we are finished
context.done(null, 'sns function finished!');
});
console.log('end of sns functions');
我得到的错误是:
ConfigError:config \\ n中缺少区域
在Request.VALIDATE_REGION(/node_modules/aws-sdk/lib/event_listeners.js:81:45)\\n
在Request.callListeners(/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\\n
at callNextListener(/node_modules/aws-sdk/lib/sequential_executor.js:95:12)\\n
at /node_modules/aws-sdk/lib/event_listeners.js:75:9\\n
在完成时(/node_modules/aws-sdk/lib/config.js:228:7)\\n
at /node_modules/aws-sdk/lib/config.js:268:9\\n
at resolveNext(/node_modules/aws-sdk/lib/credentials/credential_provider_chain.js:84:9)\\n
at /node_modules/aws-sdk/lib/credentials/credential_provider_chain.js:97:11 \\ n
at /node_modules/aws-sdk/lib/credentials.js:123:23 \\ n
at /node_modules/aws-sdk/lib/credentials/ec2_metadata_credentials.js:66:7 \\“\”,\“ip \”:\“127.0.0.1 \”}“,
即使我正在调用AWS.config.update,为什么我会这样做。
答案 0 :(得分:0)
IRAM,
如果我将您的确切代码粘贴到Lambda Node.js 4.3函数中并从Lambda控制台执行测试,结果就是:
错误:InvalidParameter:参数无效:TopicArn原因:指定了TopicArn和TargetArn。仅使用一个或另一个
这意味着在params中,您需要注释掉TopicArn或TargetArn,或者输入一些逻辑来确定传入的有效负载是否包含作为目标端点或主题端点的Arn。
您仍然可以将Lambda执行角色的权限问题发送到SNS或CW日志,但是,无论您是否有权从Lambda函数发布日志或将日志发送到CloudWatch,始终从控制台运行测试吐出一些记录正在发生的事情。
祝你好运。答案 1 :(得分:0)
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
指定像这样的aws sdk使用的区域