Wasyster
Wasyster

Reputation: 2535

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. 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

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

enter image description here

But now Azure is throwing an exception:

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

Did I miss something? According to this page, I built it wrong!

Upvotes: 3

Views: 940

Answers (3)

Wasyster
Wasyster

Reputation: 2535

The correct JSON format for GCM is:

    {
        "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:

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

Upvotes: 2

athina.bikaki
athina.bikaki

Reputation: 809

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.

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

In this solution I have used the Newtonsoft's JSON serializer to serialize my JObject.

Upvotes: 1

Tom Sun
Tom Sun

Reputation: 24539

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. 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. 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.

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.

Upvotes: 1

Related Questions