Geo*_*kos 4 .net c# amazon-web-services amazon-sns asp.net-core
我正在尝试通过 Amazon SNS 向移动设备发送远程通知。我有一个数据库,用于存储需要提供给 SNS 的 PublishRequest 的 JSON(有效负载)。我在代码中序列化 json 并将其传递给请求。
问题是 SNS 失败并出现错误:“MESSAGE STRUCTURE - JSON MESSAGE BODY FAILED TO PARSE”
作为要求,服务(负责与 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);
Run Code Online (Sandbox Code Playgroud)
来自数据库的 JSON:
{"APNS": {"aps": {"alert": "Check out the new!", "sound": "default"}, "category": {"type": "sports"}}}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个但没有任何运气:
{"default": "something", "APNS": {"aps": {"alert": "Check out the new games!", "sound": "default"}, "game": {"type": "Xbox"}}}
Run Code Online (Sandbox Code Playgroud)
最后我想通了,也许这个答案会对某人有所帮助。数据库中的 JSON 应该是
{"aps": {"alert": "Check out the new!", "sound": "default"}, "category": {"type": "sports"}}
Run Code Online (Sandbox Code Playgroud)
.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);
Run Code Online (Sandbox Code Playgroud)
AWSRoot 是我们为 SNS 创建的根对象
public class AWSRoot
{
public string APNS { get; set; }
public AWSRoot(string payload)
{
APNS = payload;
}
}
Run Code Online (Sandbox Code Playgroud)