Search code examples
c#push-notificationamazon-sns

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.
  2. Setup Amazon SNS: Mobile:Push notifications.
  3. I am able to publish message using Amazon SNS Console through "Custom payload for each delivery protocol".

{ "GCM": "{ "data": { "MyTitle": "Sample endpoints", "MyMessage": "Important" } }" }

Amazon SNS Console - Publish Message

  1. My running Android App is able to recieve the message with "data" only. This is what I expected to recieve.

"{MyTitle=Sample endpoints, MyMessage=Important}"

Android Studio - Message received

  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. I am able to published successfully.

Visual studio .NET Application

  1. My running Android App recieved the message but the message is wrapped within the "default" attribute. This is not expected, I want the same message I recieved from the Amazon SNS Console.

{default={"GCM": "{ "data": {"MyTitle": "Sample endpoints", "MyMessgae" : "Criticald" } } " }}

Android Studio - Message received

  1. I tried to utilize PublishRequest.MessageSturcture to "json" (this requires "default" attribute before it can be published). 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? I want to publish to the "platform endpoint ARN".

Solution

  • Sending the message from the AWS Console is different from sending the message using AmazonSimpleNotificationServiceClient.

    You will need to construct "PublishRequest" object.

    • Set "MessageStructure = json"
    • Use TargetArn / TopicArn.
    • Message = needs to be in specific format. 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.

    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