【发布时间】:2018-04-06 19:57:08
【问题描述】:
我正在尝试从我的后端应用程序向 Android 手机发送通知。我设法安装设备并删除它们。现在我遇到了消息有效负载的问题。我需要声音警报,并且我需要在消息中发送一些应用程序数据。这就是我现在构建payload的方式,但我认为它并不好:
string notificationText = NotificationText(story, profile);
JProperty messageJProperty = new JProperty("message", notificationText);
JObject messageJObject = new JObject(messageJProperty);
JProperty objectJProperty = new JProperty("data", messageJObject);
JObject message = new JObject(objectJProperty);
var payload = message.ToString();
return payload;
谢谢
更新(2017 年 11 月 3 日): 我发现 Azure 会接受这种有效载荷格式:
private string Payload(string notificationText, StoryEntity story, ProfileEntity profile, string deviceToken)
{
var payload = new JObject
(
new JProperty("registration_ids", new JArray(deviceToken)),
new JProperty("data", new JObject(
new JProperty("title", "Mapporia has new stroy>"),
new JProperty("message", notificationText)
)),
new JProperty("notId", $"{new Random().Next(int.MaxValue)}"),
new JProperty("content-available", 1),
new JProperty("soundname", "default"),
new JProperty("image", @"www/assets/img/logo.png"),
new JProperty("image-type", "circle"),
new JProperty("style", "inbox"),
new JProperty("notData", new JObject(
new JProperty("storyId", story.Id),
new JProperty("profileId", profile.Id)
))
).ToString(Newtonsoft.Json.Formatting.None);
return payload;
}
这就是我的 json 的样子:
但现在 Azure 正在抛出异常:
1 2017-11-01 创建故事:远程服务器返回错误:(400) 错误的请求。提供的通知有效载荷是 invalid.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 晚上 9:53:07
我错过了什么吗? 根据这个page,我建错了!
【问题讨论】:
标签: c# google-cloud-messaging asp.net-core-mvc azure-notificationhub