Search code examples
c#.netamazon-web-servicesasp.net-coreamazon-sns

SNS - JSON message body failed to parse


I am trying to send to mobile devices a remote notification through Amazon SNS. I got a database which i store the JSON (payload) which needs to be given to PublishRequest of the SNS. I serialise the json in code and pass it to the request.

The issue is that SNS fails with an error: "MESSAGE STRUCTURE - JSON MESSAGE BODY FAILED TO PARSE"

As a requirement, the service (which is responsible to communicate with SNS and send the notification) has to retrieve from DB (MySQL) the json.

What I am missing?

The database is MySQL and the service is written in .Net Core

string messageFromDb = JsonConvert.SerializeObject(input.Payload);

var request = new PublishRequest
{
    TargetArn = endpoint.EndpointArn,
    MessageStructure = "json",
    Message = messageFromDb
};

PublishResponse publishResponse = await _client.PublishAsync(request);

JSON from DB:

{"APNS": {"aps": {"alert": "Check out the new!", "sound": "default"}, "category": {"type": "sports"}}}

I tried also this without any luck:

{"default": "something", "APNS": {"aps": {"alert": "Check out the new games!", "sound": "default"}, "game": {"type": "Xbox"}}}

Solution

  • Finally I figured it out, maybe this answer will help someone. The JSON in the DB should be

    {"aps": {"alert": "Check out the new!", "sound": "default"}, "category": {"type": "sports"}}
    

    The .Net code should be:

    AWSRoot obj = new AWSRoot(input.Payload);
    
    var request = new PublishRequest
    {
        TargetArn = endpoint.EndpointArn,
        MessageStructure = "json",
        Message = JsonConvert.SerializeObject(obj)
    };
    
    PublishResponse publishResponse = await _client.PublishAsync(request);
    

    AWSRoot is the root object that we create for SNS

    public class AWSRoot
    {
        public string APNS { get; set; }
    
        public AWSRoot(string payload) 
        {
            APNS = payload;
        }
    }