【问题标题】:Google Cloud Messaging payload with application data using Azure Notification Hub使用 Azure 通知中心的带有应用程序数据的 Google Cloud Messaging 有效负载
【发布时间】: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


    【解决方案1】:

    这就是我现在构建payload的方式,但我觉得不太好

    如果理解正确并且 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) 是一项免费服务,可让开发人员在服务器和客户端应用之间发送消息。这包括从服务器到客户端应用程序的下游消息,以及从客户端应用程序到服务器的上游消息。

    例如,轻量级下游消息可以通知客户端应用程序有新数据要从服务器获取,例如“新电子邮件”通知的情况。对于即时消息传递等用例,GCM 消息可以将高达 4kb 的有效负载传输到客户端应用程序。 GCM 服务处理消息排队以及与目标客户端应用程序之间的传递的所有方面。

    【讨论】:

    • 有效载荷格式是问题,而不是如何物理构建它。
    • 我已经更新了答案。更多详细信息请参阅更新部分。
    • Thnx,我得到了这部分。我想知道如何在通知中发送应用数据,如何启用声音。
    【解决方案2】:

    您可以将有效负载简化为此调用

     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。

    【讨论】:

      【解决方案3】:

      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        相关资源
        最近更新 更多