Azure push notification hub - how to handle payload formats for both iOS and Android?

℡╲_俬逩灬. 提交于 2019-12-03 14:04:45

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

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

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!