4

We're following this tutorial: How To: Windows Azure Notification Hubs (Android Apps) for Android.

Everything works fine when structuring the notification payload as described in the guide. That is:

{
    "data": {
        "msg": "$(property1)"
    }
}

However, we'd like to extend the template to use more than one custom property in the payload. Something like:

{
  "data": {
    "msg": {
        "message": "$(property1)",
        "sender": "$(property2)"
    }
  }
}

where the back-end supplies the property values via:

Dictionary<string, string> templateValues = new Dictionary<string, string>
    {
        { "property1", "Hello world" },
        { "property2", "foo" }
    };

NotificationOutcome notificationOutcome = await Hub.SendTemplateNotificationAsync(templateValues, "test");

When registering the template in the notification hub from the mobile app we receive the following error:

"Supplied notification payload is invalid"

  • Can several properties be used in the template?
  • Should we send the property value (from the back-end) as a JSON (or other structure) string instead? What is the preferred approach? We will use the template on multiple platforms (iOS, Android)

Thanks in advance

Dene
  • 578
  • 5
  • 9
The Heatherleaf
  • 128
  • 1
  • 2
  • 11

2 Answers2

6

The payload is invalid because GCM does not support nested object in the data member. You can send the message with two property by registering for the following template:

{
   "data": {
      "message": "$(property1)",
      "sender": "$(property2)"
   }
}

In you Android receiver then you can retrieve your property with

intent.getStringExtra("property1");
Elio Damaggio
  • 862
  • 4
  • 6
  • That worked, thanks! We thought msg was required by GCM because all examples used it.. Sloppy of us – The Heatherleaf Oct 09 '13 at 11:12
  • @Elio supporting three different platforms, do I need to have the same number of properties for each template platform or can I have a different number of properties for each template? I have read many tutorial but they describe just the case of a single parameter (message) that is in common for each template platform. – systempuntoout May 01 '15 at 21:21
  • 1
    Hi, Can anyone please help me in this regards. Please provide sample how to register using android with template and also how you send the payload from server. I'm struggling for the same issue for last few days. Thanks in Advance – Abhijit Kurane Aug 19 '15 at 12:13
0

In my tests, you can add your parameters:

Template:

{
   "data": {
      "message": "$(property1)",
      "args": "$(property2)",
      "myargs": "$(property3)",
   }
}

Data:

{  
    "property1":"Jonh",    
    "property2":"1,1",
    "property3":"0",
}

Results:

intent.Extras.GetString("message");
intent.Extras.GetString("args");
intent.Extras.GetString("myargs");
rubStackOverflow
  • 5,615
  • 2
  • 29
  • 43