SNS-JSON消息正文无法解析

时间:2018-07-16 13:44:17

标签: c# .net amazon-web-services asp.net-core amazon-sns

我正在尝试通过Amazon SNS向移动设备发送远程通知。我有一个数据库,该数据库存储需要提供给SNS的PublishRequest的JSON(有效负载)。我用代码序列化了json并将其传递给请求。

问题是SNS失败,并显示以下错误:“消息结构-JSON消息正文无法解析”

根据要求,该服务(负责与SNS进行通信并发送通知)必须从DB(MySQL)中检索json。

我缺少什么?

数据库为MySQL,服务使用.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:

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

我也没有运气尝试过这个

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

1 个答案:

答案 0 :(得分:2)

最后我想通了,也许这个答案会帮助某人。数据库中的JSON应该是

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

.Net代码应为:

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 是我们为SNS创建的根对象

public class AWSRoot
{
    public string APNS { get; set; }

    public AWSRoot(string payload) 
    {
        APNS = payload;
    }
}