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

ε祈祈猫儿з 提交于 2020-01-01 04:59:08

问题


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?


回答1:


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:


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




回答3:


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


来源:https://stackoverflow.com/questions/22256606/azure-push-notification-hub-how-to-handle-payload-formats-for-both-ios-and-and

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