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?
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).
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.
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)
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.
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).
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); }
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With