9
votes

I am trying to support both iOS and Android platforms through the Azure Notification Hub.

The iOS platform expects the payload in the form:

{"aps":{"alert":"Notification Hub test notification"}}

while the Android platform expects the payload in the form:

{"data":{"message":"Notification Hub test notification"}}

I am aware that the payload can be modified to include more information but the example is sufficient for the question.

Given that I send a notification to a destination based on a Tag and I do not keep a record of which platform each push notification registration uses is the only alternative to send the notification twice, once for apple native and the second for gcm native?

hubClient.SendAppleNativeNotificationAsync(payload, tag); hubClient.SendGcmNativeNotificationAsync(payload, tag);

Or, is there a way to send a notification to Azure Notification Hub with multiple payloads and then the notification hub will use the payload appropriate for the destination device?

3

3 Answers

3
votes

The solution you present is sufficient and is the best way.

If you really want to avoid the extra call (again there is no need to make the extra calls to notification hub).

  1. when you register the device also register a "type" tag
  2. query the notification hub for the "type" tag and the other tag you want to send to

    for (Registration reg in hubClient.getRegistrationsByTag(iosTag)) { hubClient.SendAppleNativeNotificationAsync(payload, tag); }

    for (Registration reg in hubClient.getRegistrationsByTag(androidTag)) { hubClient.SendGcmNativeNotificationAsync(payload, tag); }

2
votes

I had the same problem. First I tried to solve it by using Template Notifications but I had major problems when I wanted to have correct badge and sound update on ios and android. So I switched back to native notifications for iOS and Android. My final solution to the problem is to check for the type of NotificationDescription when I send the notification. I use an enumerator to get all needed tags from the notification hub and then I check for the native type and send the notification based on this. Example Code:

if (typeof(AppleRegistrationDescription) == currentNotificationDescription.GetType())
{
    var jsonPayload = "{\"aps\" : { \"alert\" : \"" + message + "\", \"badge\" : " + badge + ", \"sound\" : \"default\" }, \"acme1\" : \"bar\", \"acme2\" : 42 }";
    await _hubClient.SendAppleNativeNotificationAsync(jsonPayload, tag);
}
else if(typeof(GcmRegistrationDescription) == currentDesc.GetType())
{
    var jsonPayload = "{\"data\" : { \"message\" : \"" + message + "\", \"badge\" : " + badge + ", \"sound\" : \"default\" }, \"acme1\" : \"bar\", \"acme2\" : 42 }";
    await _hubClient.SendGcmNativeNotificationAsync(jsonPayload, tag);
}
1
votes

You will probably have to switch to templated notifications. I understood these are 'platform independent' and can be parsed on the client in the specific application.
I only used notification hubs for Windows platform, so I might be wrong here, just wanted to give a hint. http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.servicebus.notifications.notificationhubclient.sendtemplatenotificationasync.aspx