C# 使用Azure通知中心提供应用程序数据的Google云消息有效负载

C# 使用Azure通知中心提供应用程序数据的Google云消息有效负载,c#,google-cloud-messaging,asp.net-core-mvc,azure-notificationhub,C#,Google Cloud Messaging,Asp.net Core Mvc,Azure Notificationhub,我正在尝试从后端应用程序向Android手机发送通知。我设法安装并删除了这些设备。现在我在消息负载方面遇到了问题。我需要声音警报,我需要在消息中发送一些应用程序数据。这就是我现在构建有效载荷的方式,但我认为这并不好: string notificationText = NotificationText(story, profile); JProperty messageJProperty = new JProperty("message", notificationText); JObject

我正在尝试从后端应用程序向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年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) 请求错误。提供的通知有效负载为 无效。跟踪ID:666febf6-85fe-4ebd-867d-00ce5a668809_G3,时间戳:11/1/2017 晚上9:53:07

我错过什么了吗? 根据这个,我造错了

这就是我现在构建有效载荷的方式,但我认为这并不好

如果理解正确并且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的消息

谷歌云消息(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
            }
    }
以及如何使用c#和Newtonsoft JSON nuget packege构建此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);

问题在于有效负载格式,而不是如何实际构建它。我已经更新了答案。更多详细信息请参考更新部分。Thnx,我是如何得到这部分的。我想知道如何在通知中发送应用程序数据,如何启用声音。