编程语言
首页 > 编程语言> > c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载

c#-使用Azure Notification Hub的带有应用程序数据的Google Cloud Messaging有效负载

作者:互联网

我正在尝试从后端应用程序向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的样子:

enter image description here

但是现在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.

标签:asp-net-core-mvc,google-cloud-messaging,azure-notificationhub,c
来源: https://codeday.me/bug/20191110/2015429.html