[英]Google Cloud Messaging payload with application data using Azure Notification Hub
我正在尝试从我的后端应用程序向Android手机发送通知。 我设法安装设备并将其删除。 现在我遇到消息有效负载问题。 我需要声音警报,我需要在消息中发送一些应用程序数据。 这就是我现在构建有效负载的方式,但我认为这并不好:
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;
日Thnx
更新(2017-nov-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)错误请求。 提供的通知有效负载无效.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 9:53:07 PM
我错过了什么? 根据这个页面 ,我错了!
这就是我现在建立有效载荷的方式,但我认为这并不好
如果正确理解并且如果json的结构是固定的,我们可以序列化对象来做到这一点。 以下是演示代码:
string notificationText = NotificationText(story, profile);
TestData testData = new TestData { Data = new Data { Message = notificationText }};
var payload = JsonConvert.SerializeObject(testData).ToLowerInvariant();
public class TestData
{
public Data Data;
}
public class Data
{
public string Message;
}
更新:
GCM消息可以为客户端应用程序提供高达4kb的有效负载,我们可以从本教程获得有关GCM消息的更多信息。限制为4kb,不能更大。 如果你需要发送声音,我的建议是发送自定义json,并带有指向包含二进制数据的URL的消息。
Google Cloud Messaging(GCM)是一项免费服务,可让开发人员在服务器和客户端应用之间发送消息。 这包括从服务器到客户端应用的下游消息,以及从客户端应用到服务器的上游消息。
例如,轻量级下游消息可以通知客户端应用程序从服务器获取新数据,如“新电子邮件”通知的情况。 对于即时消息传递等用例,GCM消息可以将最多4kb的有效负载传输到客户端应用程序。 GCM服务处理消息排队以及与目标客户端应用程序之间的传递的所有方面。
您可以简化此次调用的有效负载
var payload = new JObject(
new JProperty("data", new JObject(
new JProperty("message", notificationText))))
.ToString(Newtonsoft.Json.Formatting.None);
当GCM接受它时,输出将是JSON格式的有效负载。
{"data":{"message":"your notification Text"}}
在这个解决方案中,我使用了Newtonsoft的JSON序列化程序来序列化我的JObject。
GCM的正确JSON格式是:
{
"to" : "{{devicetoken}} OR {{registrationID form Azure}}",
"data":
{
"title":"{{title goes here}}",
"message":"{{message body goes here}}",
"priority":"high"
},
"notId":"{{unique ID, I used RANDOM to generate it}}",
"content-available":1,
"soundname":"default",
"image":"www/assets/img/logo.png",
"image-type":"circle",
"style":"inbox",
"notData":
{
"storyId":1,
"profileId":6
}
}
以及如何使用带有Newtonsoft JSON nuget packege的c#构建此JSON:
var payload = new JObject
(
new JProperty("to", deviceToken),
new JProperty("data", new JObject(
new JProperty("title", "title goes here"),
new JProperty("message", "notification text goes here"),
new JProperty("priority", "high")
)),
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.