node.js 如何将 sns 发布到特定端点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19361809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to publish sns to a specific endpoint?
提问by Alex Cebotari
I have a issue with publishing sns to a specific endpoint.
我在将 sns 发布到特定端点时遇到问题。
My code:
我的代码:
var AWS = require('aws-sdk');
AWS.config.loadFromPath('/web/config.json');
var sns = new AWS.SNS();
sns.publish({
// TopicArn:'arn:aws:sns:us-west-2:302467918846:MyTestTopik',
TargetArn: 'arn:aws:sns:us-west-2:302467918846:MyTestTopik:613ee49c-d4dc-4354-a7e6-c1d9d8277c56',
Message: "Success!!! ",
Subject: "TestSNS"
}, function(err, data) {
if (err) {
console.log("Error sending a message " + err);
} else {
console.log("Sent message: " + data.MessageId);
}
});
When I use TopicArn, everything is fine. But when I try to send notification to a specific endpoint I take error:
当我使用时TopicArn,一切都很好。但是当我尝试向特定端点发送通知时,我出错了:
Error sending a message InvalidParameter: Invalid parameter: Topic Name
发送消息时出错 InvalidParameter: Invalid parameter: Topic Name
And I have no idea what kind of parameters it is and from where.
而且我不知道它是什么类型的参数以及来自哪里。
回答by guya
Something similar is working fine for me. I'm able to publish to a specific endpoint using: Apple Push Notification Service Sandbox (APNS_SANDBOX)
类似的东西对我来说很好用。我可以使用以下命令发布到特定端点:Apple Push Notification Service Sandbox (APNS_SANDBOX)
You might also want to try and update the aws-sdk, current version is 1.9.0.
您可能还想尝试更新 aws-sdk,当前版本是 1.9.0。
Here's my code, TargetArn was copied directly from the SNS console. I omitted some of the data, like &
这是我的代码,TargetArn 是直接从 SNS 控制台复制的。我省略了一些数据,比如 &
var sns = new AWS.SNS();
var params = {
TargetArn:'arn:aws:sns:us-west-2:302467918846:endpoint/APNS_SANDBOX/<APP_NAME>/<USER_TOKEN>'
Message:'Success!!! ',
Subject: 'TestSNS'
};
sns.publish(params, function(err,data){
if (err) {
console.log('Error sending a message', err);
} else {
console.log('Sent message:', data.MessageId);
}
});
回答by Click Ahead
You might have an invalid Region. Check you Region for the Topic and set it accordingly. For example if you are us-west-2 you could do something like
您的区域可能无效。检查您的主题区域并相应地设置它。例如,如果你是 us-west-2 你可以做类似的事情
var sns = new aws.SNS({region:'us-west-2'});
回答by E.T
None of this will work if you don't massage the payload a bit.
如果你不稍微按摩一下有效载荷,这些都不会起作用。
var arn = 'ENDPOINT_ARN';
console.log("endpoint arn: " + arn);
var payload = {
default: message_object.message,
GCM: {
data: {
message: message_object.message
}
}
};
// The key to the whole thing is this
//
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
// Create the params structure
//
var params= {
TargetArn: arn,
Message: payload,
MessageStructure: 'json' // Super important too
};
sns.publish(params , function(error, data) {
if (error) {
console.log("ERROR: " + error.stack);
}
else {
console.log("data: " + JSON.stringify(data));
}
context.done(null, data);
});
So, it turns out that you have tospecify the message structure (being json). I tried to publish to endpoint from the AWS console and it worked great as long as I selected JSON. Using RAW would do nothing.
因此,事实证明您必须指定消息结构(即 json)。我尝试从 AWS 控制台发布到端点,只要我选择 JSON,它就可以很好地工作。使用 RAW 将无济于事。
In my script was doing was the previous posts were doing:
在我的脚本中所做的是以前的帖子所做的:
var params = {
TargetArn: arn,
Message:'Success!!! ',
Subject: 'TestSNS'
};
And even though CloudWatch was logging success, I never once got the message. As soon as I added the MessageStructure data and that I properly formatted the payload, it worked like a charm.
即使 CloudWatch 记录成功,我也从未收到过该消息。一旦我添加了 MessageStructure 数据并正确格式化了有效负载,它就像一个魅力。
The [default] parameter is not useful but I left it in there to show what the structure could look like.
[default] 参数没有用,但我将它留在那里以显示结构的外观。
If you don't stringify the payload.GCM part, SNS will barf and say that your message should include a "GCM" element.
如果您没有对 payload.GCM 部分进行字符串化,SNS 将拒绝并说您的消息应包含“GCM”元素。
The only thing that is annoying is that you are required to know what the endpoint is. I was hoping that you didn't have to format the message based on the endpoint, which really defeats the purpose of SNS in some ways.
唯一令人讨厌的是,您需要知道端点是什么。我希望您不必根据端点格式化消息,这在某些方面确实违背了 SNS 的目的。
回答by Neo
Are you trying endpoints other that push notifications such as sms? Direct addressing is currently only available for push notifications endpoints. That is the error you will get when you try to publish to a specific endpoint that does not allow direct direct addressing!
您是否正在尝试推送通知等其他端点?直接寻址目前仅适用于推送通知端点。这就是当您尝试发布到不允许直接直接寻址的特定端点时将得到的错误!
http://aws.amazon.com/sns/faqs/#Does_SNS_support_direct_addressing_for_SMS_or_Email
http://aws.amazon.com/sns/faqs/#Does_SNS_support_direct_addressing_for_SMS_or_Email
回答by Minor Solis
I was having the exact same issue as you. The problem is the TargetArn that you're using, there's not clear documentation about it. Error happens if you try to put the Application ARN in the TargetArn.
我和你遇到了完全相同的问题。问题在于您使用的 TargetArn,没有关于它的明确文档。如果您尝试将应用程序 ARN 放在 TargetArn 中,则会发生错误。
That will produce the error: Invalid parameter: TargetArn Reason: >arn:aws:sns:us-west-2:561315416312351:app/APNS_SANDBOX/com.APP_NAME_HERE.app is >not a valid ARN to publish to.
这将产生错误: Invalid parameter: TargetArn Reason: >arn:aws:sns:us-west-2:561315416312351:app/APNS_SANDBOX/com.APP_NAME_HERE.app is >not a valid ARN to publish to。
All you need to do is to put the EndpointArn in the TargetArn.
您需要做的就是将 EndpointArn 放在 TargetArn 中。
If you need to see the EndpointArn, you can:
如果您需要查看 EndpointArn,您可以:
- Call listPlatformApplications() to get all your applications ARN's.
- Call listEndpointsByPlatformApplication() with the App ARN to get the EndpointArn's list.
- 调用 listPlatformApplications() 以获取您的所有应用程序 ARN。
- 使用 App ARN 调用 listEndpointsByPlatformApplication() 以获取 EndpointArn 的列表。
Enjoy!
享受!

