Amazon SNS - 向多个设备发送推送通知
Amazon SNS - Sending push notifications to several devices
我正在尝试一次性向多个设备发送推送通知。为此,我正在执行以下操作:
- 列表项
- 创建主题
- 订阅该主题的所有我想要的设备
- 向主题发布消息
我正在尝试向我的应用程序发送自定义 JSON。内容为:
{"APNS_SANDBOX":"{\"aps\":\"{\\"u\\":\\"1\\"}\"}"}
不过,我在发送 JSON 时遇到 InvalidParameter
错误。错误详细信息是
"Message Structure - No default entry in JSON message body"
奇怪的是,发送到单个设备(发布到 endpointarn)的同一个 JSON 正在工作。
这是我的代码:
AmazonSimpleNotificationServiceClient client = new AmazonSimpleNotificationServiceClient(ssAmazonToken.ssSTAmazonToken.ssAccessKey, ssAmazonToken.ssSTAmazonToken.ssSecretKey, solveRegionEndpoint(ssRegionEndpoint));
//create topic
CreateTopicRequest topicRequest = new CreateTopicRequest();
string topicName = Guid.NewGuid().ToString();
topicRequest.Name = topicName;
log(ssIsDebugMode, "Name (" + topicRequest.Name + ")", module);
CreateTopicResponse topicResponse = client.CreateTopic(topicRequest);
ssTopicArn = topicResponse.TopicArn;
//subscribe endpoints to the topic
foreach(RCAmazonSNSDeviceRecord endpoint in ssDevices)
{
SubscribeRequest subscribeRequest = new SubscribeRequest();
subscribeRequest.TopicArn = topicResponse.TopicArn;
subscribeRequest.Endpoint = endpoint.ssSTAmazonSNSDevice.ssEndpointArn;
subscribeRequest.Protocol = "application";
log(ssIsDebugMode, "TopicArn (" + subscribeRequest.TopicArn + ") "
+ "Endpoint (" + subscribeRequest.Endpoint + ") "
+ "Protocol (" + subscribeRequest.Protocol + ") ", module);
SubscribeResponse subscribeResponse = client.Subscribe(subscribeRequest);
/*ConfirmSubscriptionRequest confirmSubsRequest = new ConfirmSubscriptionRequest();
confirmSubsRequest.AuthenticateOnUnsubscribe = true;
confirmSubsRequest.TopicArn = topicResponse.TopicArn;*/
}
//publish message to the topic
PublishRequest publishRequest = new PublishRequest();
publishRequest.TopicArn = topicResponse.TopicArn;
publishRequest.MessageStructure = ssIsJSON ? "json" : "";
publishRequest.Message = ssMessageContent;
log(ssIsDebugMode, "TargetArn (" + publishRequest.TargetArn + ") "
+ "MessageStructure (" + publishRequest.MessageStructure + ") "
+ "Message (" + publishRequest.Message + ") ", module);
PublishResponse response = client.Publish(publishRequest);
ssAmazonResponse.ssSTAmazonResponse.ssResponseCode = response.HttpStatusCode.ToString();
ssMessageId = response.MessageId;
ssContentLength = response.ContentLength.ToString();
SNS 要求 Message
JSON 中有一个名为 "default"
的顶级属性,如果将 MessageStructure
设置为json
。来自 Publish
API documentation(强调我的):
MessageStructure
Set MessageStructure
to json
if you want to send a
different message for each protocol. For example, using one publish
action, you can send a short message to your SMS subscribers and a
longer message to your email subscribers. If you set MessageStructure
to json
, the value of the Message parameter must:
- be a syntactically valid JSON object;
- and contain at least a top-level JSON key of "default" with a value that is a string.
You can define other top-level keys that define the message you want to send to a specific transport protocol (e.g., "http").
For information about sending different messages for each protocol
using the AWS Management Console, go to Create Different Messages for
Each Protocol in the Amazon Simple Notification Service Getting
Started Guide.
Valid value: json
Type: String
Required: No
因此,在向主题发送 SNS 通知时,无法保证所有订阅者都使用 APNS 消息服务。
因此,SNS 要求您在传递给它的 JSON 中也包含一个 'default' 字段。此默认字段将用于不属于 APNS 消息服务的所有其他订阅者。
因此,在这种情况下,您需要添加另一个 JSON 元素,以便在非 APNS 订阅者时提供默认消息。
{
"default": "Enter default message here",
"APNS_SANDBOX": "{\"aps\":\"{\\"u\":\\"1\\"}\"}"
}
在此页面底部可以找到更多示例:
http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html
我正在尝试一次性向多个设备发送推送通知。为此,我正在执行以下操作:
- 列表项
- 创建主题
- 订阅该主题的所有我想要的设备
- 向主题发布消息
我正在尝试向我的应用程序发送自定义 JSON。内容为:
{"APNS_SANDBOX":"{\"aps\":\"{\\"u\\":\\"1\\"}\"}"}
不过,我在发送 JSON 时遇到 InvalidParameter
错误。错误详细信息是
"Message Structure - No default entry in JSON message body"
奇怪的是,发送到单个设备(发布到 endpointarn)的同一个 JSON 正在工作。
这是我的代码:
AmazonSimpleNotificationServiceClient client = new AmazonSimpleNotificationServiceClient(ssAmazonToken.ssSTAmazonToken.ssAccessKey, ssAmazonToken.ssSTAmazonToken.ssSecretKey, solveRegionEndpoint(ssRegionEndpoint));
//create topic
CreateTopicRequest topicRequest = new CreateTopicRequest();
string topicName = Guid.NewGuid().ToString();
topicRequest.Name = topicName;
log(ssIsDebugMode, "Name (" + topicRequest.Name + ")", module);
CreateTopicResponse topicResponse = client.CreateTopic(topicRequest);
ssTopicArn = topicResponse.TopicArn;
//subscribe endpoints to the topic
foreach(RCAmazonSNSDeviceRecord endpoint in ssDevices)
{
SubscribeRequest subscribeRequest = new SubscribeRequest();
subscribeRequest.TopicArn = topicResponse.TopicArn;
subscribeRequest.Endpoint = endpoint.ssSTAmazonSNSDevice.ssEndpointArn;
subscribeRequest.Protocol = "application";
log(ssIsDebugMode, "TopicArn (" + subscribeRequest.TopicArn + ") "
+ "Endpoint (" + subscribeRequest.Endpoint + ") "
+ "Protocol (" + subscribeRequest.Protocol + ") ", module);
SubscribeResponse subscribeResponse = client.Subscribe(subscribeRequest);
/*ConfirmSubscriptionRequest confirmSubsRequest = new ConfirmSubscriptionRequest();
confirmSubsRequest.AuthenticateOnUnsubscribe = true;
confirmSubsRequest.TopicArn = topicResponse.TopicArn;*/
}
//publish message to the topic
PublishRequest publishRequest = new PublishRequest();
publishRequest.TopicArn = topicResponse.TopicArn;
publishRequest.MessageStructure = ssIsJSON ? "json" : "";
publishRequest.Message = ssMessageContent;
log(ssIsDebugMode, "TargetArn (" + publishRequest.TargetArn + ") "
+ "MessageStructure (" + publishRequest.MessageStructure + ") "
+ "Message (" + publishRequest.Message + ") ", module);
PublishResponse response = client.Publish(publishRequest);
ssAmazonResponse.ssSTAmazonResponse.ssResponseCode = response.HttpStatusCode.ToString();
ssMessageId = response.MessageId;
ssContentLength = response.ContentLength.ToString();
SNS 要求 Message
JSON 中有一个名为 "default"
的顶级属性,如果将 MessageStructure
设置为json
。来自 Publish
API documentation(强调我的):
MessageStructure
Set
MessageStructure
tojson
if you want to send a different message for each protocol. For example, using one publish action, you can send a short message to your SMS subscribers and a longer message to your email subscribers. If you setMessageStructure
tojson
, the value of the Message parameter must:
- be a syntactically valid JSON object;
- and contain at least a top-level JSON key of "default" with a value that is a string.
You can define other top-level keys that define the message you want to send to a specific transport protocol (e.g., "http").
For information about sending different messages for each protocol using the AWS Management Console, go to Create Different Messages for Each Protocol in the Amazon Simple Notification Service Getting Started Guide.
Valid value: json
Type: String
Required: No
因此,在向主题发送 SNS 通知时,无法保证所有订阅者都使用 APNS 消息服务。 因此,SNS 要求您在传递给它的 JSON 中也包含一个 'default' 字段。此默认字段将用于不属于 APNS 消息服务的所有其他订阅者。
因此,在这种情况下,您需要添加另一个 JSON 元素,以便在非 APNS 订阅者时提供默认消息。
{ "default": "Enter default message here", "APNS_SANDBOX": "{\"aps\":\"{\\"u\":\\"1\\"}\"}" }
在此页面底部可以找到更多示例: http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html