Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 688
user1282345 Avatar asked Mar 07 '14 17:03

user1282345


People also ask

What is the payload size of notification in iOS?

Apple Push Notification service (APNs) refuses a notification if the total size of its payload exceeds the following limits: For Voice over Internet Protocol (VoIP) notifications, the maximum payload size is 5 KB (5120 bytes). For all other remote notifications, the maximum payload size is 4 KB (4096 bytes).

How many types of push notification are there in iOS?

Types of Push Notifications Push notifications are often classified into two primary categories based on the intended use case and message content: transactional and marketing notifications.

What is push notification payload maximum size?

Android 12(vanilla/stock) Collapsed notification: body - 43 characters(1 line), header - 39 characters(1 line) Extended notification: body - 504 characters(10 lines), header - 79 characters(2 lines) Notification with banner(image): body - 96 characters(2 lines), header -79 characters(2 lines)

Which dependency managers is the notification hubs SDK available through?

The Azure Notification Hubs SDK also supports the Swift Package Manager. To integrate, use the following steps: From the Xcode menu click File > Swift Packages > Add Package Dependency.


2 Answers

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

like image 156
Wade Anderson Avatar answered Oct 04 '22 17:10

Wade Anderson


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);
}
like image 30
Freddy Avatar answered Oct 04 '22 17:10

Freddy