简体   繁体   English

使用Azure Notification Hub的Google Cloud Messaging有效负载和应用程序数据

[英]Google Cloud Messaging payload with application data using Azure Notification Hub

I am trying to send notification from my backend application to Android mobile phones. 我正在尝试从我的后端应用程序向Android手机发送通知。 I managed to install devices and remove them. 我设法安装设备并将其删除。 Now I have problems with the message payload. 现在我遇到消息有效负载问题。 I need sound alert, and I need to send some application data in the message. 我需要声音警报,我需要在消息中发送一些应用程序数据。 This is how I build the payload now, but I think it's not good: 这就是我现在构建有效负载的方式,但我认为这并不好:

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 日Thnx

update (2017-nov-3): I found that this payload format will be accepted by Azure: 更新(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;
    }

This is how my json looks like: 这就是我的json的样子:

在此输入图像描述

But now Azure is throwing an exception: 但现在Azure正在抛出异常:

1 2017-11-01 Create Story : The remote server returned an error: (400) Bad Request. 1 2017-11-01创建故事:远程服务器返回错误:(400)错误请求。 The supplied notification payload is invalid.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 9:53:07 PM 提供的通知有效负载无效.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 9:53:07 PM

Did I miss something? 我错过了什么? According to this page , I built it wrong! 根据这个页面 ,我错了!

This is how I build the payload now, but I think it's not good 这就是我现在建立有效载荷的方式,但我认为这并不好

If understanding correctly and if the struction of the json is fixed, we could serialize object to do that. 如果正确理解并且如果json的结构是固定的,我们可以序列化对象来做到这一点。 The following is the demo code: 以下是演示代码:

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

Updated: 更新:

A GCM message can be up to 4kb of payload to the client app, we could get more info about GCM message from this tutorial .The limitation is 4kb and cannot be any larger. GCM消息可以为客户端应用程序提供高达4kb的有效负载,我们可以从本教程获得有关GCM消息的更多信息。限制为4kb,不能更大。 If you need to send a sound, my suggestion is that to send custom json with the message that points to a URL that houses the binary data. 如果你需要发送声音,我的建议是发送自定义json,并带有指向包含二进制数据的URL的消息。

Google Cloud Messaging (GCM) is a free service that enables developers to send messages between servers and client apps. Google Cloud Messaging(GCM)是一项免费服务,可让开发人员在服务器和客户端应用之间发送消息。 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. 对于即时消息传递等用例,GCM消息可以将最多4kb的有效负载传输到客户端应用程序。 The GCM service handles all aspects of queueing of messages and delivery to and from the target client app. GCM服务处理消息排队以及与目标客户端应用程序之间的传递的所有方面。

You can simplify your payload to this call 您可以简化此次调用的有效负载

 var payload = new JObject(
                      new JProperty("data", new JObject(
                      new JProperty("message", notificationText))))
                      .ToString(Newtonsoft.Json.Formatting.None);

The output will be the JSON formatted payload as GCM accepts it. 当GCM接受它时,输出将是JSON格式的有效负载。

{"data":{"message":"your notification Text"}}

In this solution I have used the Newtonsoft's JSON serializer to serialize my JObject. 在这个解决方案中,我使用了Newtonsoft的JSON序列化程序来序列化我的JObject。

The correct JSON format for GCM is: 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
            }
    }

And how to build this JSON using c# with Newtonsoft JSON nuget packege: 以及如何使用带有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.

相关问题 Firebase云消息传递和Azure通知中心错误 - Firebase Cloud messaging and Azure Notification Hub Error 使用Azure通知中心的InvalidDataContractException - InvalidDataContractException using Azure Notification Hub Firebase Cloud消息传递和Azure Notification Hubs设备注册相互实现 - Firebase Cloud messaging and Azure Notification Hubs device registration mutual implementation 有没有一种方法可以将推送通知从Web门户发送到Google Cloud Messaging? - Is there a way to send push notification from a web portal to Google Cloud Messaging? 如何在不更新IOS应用程序的情况下使用Azure Notification Hub - How to Use Azure Notification Hub without updating IOS Application 使用Azure Notification Hub推送到C#WinForms应用 - Using Azure Notification Hub to push to C# WinForms app 可以使用.NET桌面应用程序作为Google Cloud Messaging通知的接收者吗? - Is it possible to use a .NET desktop application as the receiver for Google Cloud Messaging notifications? 天蓝色通知中心与templateHub发送 - azure notification hub send with templateHub Azure通知中心错误401 - Azure Notification Hub error 401 如何使用 Azure 通知中心向特定 APNS 通知令牌发送通知 - How to send a notification to a specific APNS notification token using Azure Notification Hub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM