c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2954字,纯文字阅读大概需要5分钟。
内容图文
![c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载](/upload/InfoBanner/zyjiaocheng/668/e5db819031ea4a3ab21838dd2ebab05c.jpg)
我正在尝试从后端应用程序向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;
n
更新(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 Create Story : The remote server returned an error: (400)
Bad Request. The supplied notification payload is
invalid.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017
9:53:07 PM
我错过了什么?
根据这个page,我把它弄错了!
解决方法:
This is how I build the payload now, but I think it’s not good
如果正确理解并且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的有效负载到客户端应用程序,我们可以从tutorial获取更多有关GCM消息的信息.限制为4kb,并且不能更大.如果您需要发送声音,我的建议是发送带有指向指向包含二进制数据的URL的消息的自定义json.
Google Cloud Messaging (GCM) is a free service that enables developers to send messages between servers and client apps. This includes downstream messages from servers to client apps, and upstream messages from client apps to servers.
For example, a lightweight downstream message could inform a client app that there is new data to be fetched from the server, as in the case of a “new email” notification. For use cases such as instant messaging, a GCM message can transfer up to 4kb of payload to the client app. The GCM service handles all aspects of queueing of messages and delivery to and from the target client app.
内容总结
以上是互联网集市为您收集整理的c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载全部内容,希望文章能够帮你解决c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。