9

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?

user1282345
  • 251
  • 3
  • 6

3 Answers3

3

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

Wade Anderson
  • 2,461
  • 1
  • 20
  • 22
2

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);
}
Freddy
  • 1,022
  • 9
  • 16
  • 1
    Hi @Freddy, can you please say what major problems did you have with Template Notifications? You said you wanted to have correct badge and sound update on ios and android. I don't understand, why couldn't you do that with Templates? I'm currently trying to see different aspects with push notifications, before starting to implement. Thanks! – WriteEatSleepRepeat Apr 24 '15 at 20:35
  • In former versions of the Azure Notification Hub I was only able to send simple push notifications by using templates. That means without sound and without a badge number. Maybe I just didn't understand how to configure my templates correctly but it was a mess to find out how to do it. I saw there are changes now with the Azure NotificationHub and maybe docu. I switched then to native messages because templates didn't work for me with sound and badge (it worked for ios OR for ANDROID but not for both at the same time!). With native pushs all works fine. If you have solved it please tell me! – Freddy Sep 17 '15 at 21:38
  • @Freddy I've talked to MS about this and triggering the sound on apple is not supported with templated notifications. Personally I think this is ridiculous, but they don't seem to care. – m4tt1mus Apr 23 '18 at 18:57
1

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

Sam Vanhoutte
  • 3,247
  • 27
  • 48
  • 1
    Sam is correct and you'll want to use Templates. Check out this video for an example of templates in action to see how you can use them: http://channel9.msdn.com/Shows/Cloud+Cover/Episode-116-Cross-Platform-Notifications-using-Windows-Azure-Notifications-Hub – Chris Mar 10 '14 at 09:33