简体   繁体   English

AmazonSimpleNotificationServiceClient - 如何使用包含没有“默认”值的自定义负载的 TargetArn 发布请求?

[英]AmazonSimpleNotificationServiceClient - How to PublishRequest with TargetArn that contains Custom Payload without "default" value?

I have:我有:

  1. Written a running Android App that recieved FCM notification.编写了一个正在运行的 Android 应用程序,它收到了 FCM 通知。
  2. Setup Amazon SNS: Mobile:Push notifications.设置 Amazon SNS:移动:推送通知。
  3. I am able to publish message using Amazon SNS Console through "Custom payload for each delivery protocol".我能够通过“为每个传输协议自定义负载”使用 Amazon SNS 控制台发布消息。

{ "GCM": "{ "data": { "MyTitle": "Sample endpoints", "MyMessage": "Important" } }" } { "GCM": "{ "data": { "MyTitle": "示例端点", "MyMessage": "重要" } }" }

Amazon SNS Console - Publish Message Amazon SNS 控制台 - 发布消息

  1. My running Android App is able to recieve the message with "data" only.我正在运行的 Android 应用程序只能收到带有“数据”的消息。 This is what I expected to recieve.这是我期望收到的。

"{MyTitle=Sample endpoints, MyMessage=Important}" “{MyTitle=示例端点,MyMessage=重要}”

Android Studio - Message received Android 工作室 - 收到消息

  1. Wrote a .NET application using "AmazonSimpleNotificationServiceClient" [AWSSDK.SimpleNotificationService 3.7.4.3] to publish request, trying to do the exact same way to publish message like Amazon SNS Console.使用“AmazonSimpleNotificationServiceClient”[AWSSDK.SimpleNotificationService 3.7.4.3] 编写了一个 .NET 应用程序来发布请求,尝试以完全相同的方式发布消息,例如 Amazon SNS 控制台。 I am able to published successfully.我能够成功发布。

Visual studio .NET Application视觉工作室 .NET 应用

  1. My running Android App recieved the message but the message is wrapped within the "default" attribute.我正在运行的 Android 应用程序收到了消息,但该消息包含在“默认”属性中。 This is not expected, I want the same message I recieved from the Amazon SNS Console.这不是预期的,我想要从 Amazon SNS 控制台收到的相同消息。

{default={"GCM": "{ "data": {"MyTitle": "Sample endpoints", "MyMessgae": "Criticald" } } " }} {default={"GCM": "{ "data": {"MyTitle": "示例端点", "MyMessgae": "Criticald" } } " }}

Android Studio - Message received Android 工作室 - 收到消息

  1. I tried to utilize PublishRequest.MessageSturcture to "json" (this requires "default" attribute before it can be published).我尝试将 PublishRequest.MessageSturcture 用于“json”(这需要“默认”属性才能发布)。 I removed that, the message automatically wrapped with "default".我删除了它,消息自动用“默认”包装。
  2. How do I send custom raw message from .NET Application the same way I can send "Custom Payload for each delivery protocol" from Amazon SNS Console?如何从 .NET 应用程序发送自定义原始消息,就像我从 Amazon SNS 控制台发送“每个传输协议的自定义负载”一样? I want to publish to the "platform endpoint ARN".我想发布到“平台端点 ARN”。

Sending the message from the AWS Console is different from sending the message using AmazonSimpleNotificationServiceClient.从 AWS 控制台发送消息不同于使用 AmazonSimpleNotificationServiceClient 发送消息。

You will need to construct "PublishRequest" object.您将需要构建“PublishRequest”object。

  • Set "MessageStructure = json"设置“MessageStructure = json”
  • Use TargetArn / TopicArn.使用 TargetArn / TopicArn。
  • Message = needs to be in specific format. Message = 需要采用特定格式。 It is mandatory to have "default" member with dummy data follow by "GCM" or "APNS"... The content of the "GCM" is a string and not child node.必须有“默认”成员和虚拟数据后跟“GCM”或“APNS”......“GCM”的内容是一个字符串而不是子节点。

This is a working code:这是一个工作代码:

        // Android Payload
        string payload = "\\\"data\\\":{\\\"MyTitle\\\":\\\"test message \\\", \\\"MyMessage\\\": \\\"Important\\\"}";

        // iOS Payload
        //string payload = "\\\"notification\\\" : {\\\"content_available\\\" : true }, \\\"data\\\": { \\\"MyTitle\\\": \\\"Sample message for iOS endpoints\\\", \\\"MyMessage\\\":\\\"Hello world\\\",\\\"Payload\\\": {\\\"InstructionID\\\": \\\"f7a87860-11f4-47e1-8a39-3da2ffd6eadb\\\",\\\"IsAlert\\\": false,\\\"Content\\\": \\\"This is new content\\\",\\\"Value\\\": 2.554,\\\"Counter\\\": 290,\\\"DateTime\\\": \\\"2022-09-11T16:13:57.2009482Z\\\"}}";
        var request = new PublishRequest
        {   
            Message = "{\"default\": \"default message\",\"GCM\":\"{" + payload + "}\"}",
            TargetArn = fcmARN,
            MessageStructure = "json"
        };
        
        try
        {
            var response = await snsClient.PublishAsync(request);
            if (response.HttpStatusCode.Equals(HttpStatusCode.OK))
                Console.WriteLine("Message Sent to Topic");
            else
                Console.WriteLine("Failed - Message Sent to Topic");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

Example Message: enter image description here示例消息:在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将自定义负载从 React bf DIRECT_LINE/CONNECT_FULFILLED 发送到 onMembersAdded 事件 - How to send custom payload from React bf DIRECT_LINE/CONNECT_FULFILLED to onMembersAdded event 如何覆盖自定义运算符中默认参数的参数? - How to override the parameters of default args in a custom operator? Dialogflow 自定义有效负载未在 facebook 信使中发布消息 - Dialogflow custom payload not posting message in facebook messenger 没有负载的 FCM 推送通知到 Android 设备 - FCM push notification without payload to Android device 自定义 Lambda 授权方事件负载类型 - Custom Lambda Authorizer Event Payload type 对话流自定义有效负载 json - dialogflow custom payload json 在没有默认 vpc 的情况下使用 Lambda 将自定义 cidr 添加到入口安全组 - Adding custom cidr to ingress security group using Lambda without default vpc 如何在 AndroidManifest 中使用自定义图标作为 FCM 默认通知图标 - Maui - How to use a custom icon as FCM default notification icon in AndroidManifest - Maui 如何使用 gcloud sdk 创建没有 default.network 的 gcp 项目 - how to create gcp projects without default network using gcloud sdk 如何使用@DynamoDBAutoGeneratedDefault 注释为 Boolean 字段指定默认值 - How to specify default value for Boolean field with @DynamoDBAutoGeneratedDefault annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM