gpt4 book ai didi

amazon-sns - Amazon SNS - 向多个设备发送推送通知

转载 作者:行者123 更新时间:2023-12-01 03:44:06 29 4
gpt4 key购买 nike

我正在尝试一次将推送通知发送到多个设备。为此,我正在执行以下操作:

  • 列表项
  • 创建话题
  • 订阅我想要的所有设备到该主题
  • 向主题
  • 发布消息

    我正在尝试将自定义 JSON 发送到我的应用程序。内容是:
    {"APNS_SANDBOX":"{\"aps\":\"{\\\"u\\\":\\\"1\\\"}\"}"}

    不过,我得到了 InvalidParameter发送 JSON 时出错。错误细节是

    "Message Structure - No default entry in JSON message body"



    奇怪的是,发送到单个设备(发布到端点)的相同 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 要求有一个名为 "default" 的顶级属性在 Message如果您设置 MessageStructure,则 JSON 带有默认协议(protocol)的字符串值至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

    关于amazon-sns - Amazon SNS - 向多个设备发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601639/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com