Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set high priority of push notification which is send to NotificationHub

I have successfully implemented Azure Function, which sends push notifications to NotificationHub for Android, iOS and UWP. During the testing I discovered that when Android device is in Doze mode push notification is not delivered on lock screen. It is only delivered when I unlock the phone.

I found out this stackoverflow post, which helped me to fix it with method SendFcmNativeNotificationAsync. I used this code:

string payload = @"{
                    ""data"":{
                        ""notificationtype"":""sap"",
                        ""title"":""value2"",
                    },
                        ""priority"":""high""
                    }";

await hub.SendFcmNativeNotificationAsync(payload);

However my implementation using SendNotificationAsync method which works for all 3 platforms, but not in Doze mode. This is my code to send notification:

Dictionary<string, string> templateParameters = new Dictionary<string, string>();

templateParameters["alert"] = "Test";
templateParameters["title"] = "Whosap";

var notification = new TemplateNotification(templateParameters);
await hub.SendNotificationAsync(notification);

I tried to set header of notification:

var headers = new Dictionary<string, string> { { "android", @"{""priority"": ""high""}" } };
notification.Headers = headers;

But this didn't work. Does anybody know how to set priority in TemplateNotification object and send it with SendNotificationAsync method?

This is how I force/unforce device to doze mode:

adb shell dumpsys deviceidle force-idle
adb shell dumpsys deviceidle unforce
like image 379
Jan Nepraš Avatar asked May 28 '20 14:05

Jan Nepraš


People also ask

How is priority applied on notifications?

The notification priority can be adjusted in Threema > Settings > Sound & Notification > Priority. This setting controls how prominently notifications are displayed. The default is High and should not be changed.

What is “APNs-priority” in push notification services?

Apple Push Notification Services added “ apns-priority ” to its notification send request headers a while back for users to specify send priorities with ten marking immediate and high-priority sends and five marking device power saving sends. We want to share a few sample snippets for how to use this feature with Notification Hubs' .NET SDK.

Should I Set my notification priority to high?

To sum up, you don't have to set your notification's priority to high, because the OS will see that it a simple informative notification and it will now wake the phone. Keep in mind that, when the phone goes into "deep sleep" Doze mode, it shouldn't and mustn't wake for an informative notifications, so there's no point in trying - that's by design.

How do I use X-APNs-priority with notification hubs?

We want to share a few sample snippets for how to use this feature with Notification Hubs' .NET SDK. To push without templates, simply set the X-Apns-Priority property in the notification headers before you send the push request.

How to send push notifications from Microsoft Push Notification Service (MPNS)?

Microsoft Push Notification Service (MPNS) has been deprecated and is no longer supported. The GcmService object provides a send method that can be used to send push notifications to Android applications. The send method accepts the following parameters: Tags - the tag identifier.


Video Answer


1 Answers

Based on your comment that you want to have "only informative push notifications", unfortunately, you cannot force the phone to show your notifications every time. This doesn't come as a limitation to either Xamarin, or even Azure Functions in that matter. Even some of Google's own applications behave the same.

Looking at Doze checklist:

If your users must see a notification right away, make sure to use an FCM high priority message

Even if we look at Using FCM to interact with your app while the device is idle:

FCM is optimized to work with Doze and App Standby idle modes by means of high-priority FCM messages. FCM high-priority messages let you reliably wake your app to access the network, even if the user’s device is in Doze or the app is in App Standby mode. In Doze or App Standby mode, the system delivers the message and gives the app temporary access to network services and partial wakelocks, then returns the device or app to the idle state.

So far it all looks like that if you decide to use High priority messages, everything should work fine. However, that's not the whole story. You are correct in setting the priority to high "priority":"high". Unfortunately, there are some caveats to this setting (from docs):

High priority messages generally should result in user interaction with your app or its notifications. If FCM detects a pattern in which they don't, your messages may be de-prioritized.

What this means is that if you are sending only an informative notification, it won't be enough to wake the device from Doze mode and show the notification. There checks/restrictions got even tighter from Android P. Read about app standby buckets here.

The good thing is that exiting Doze mode comes in a form of various events/interactions:

As soon as the user wakes the device by moving it, turning on the screen, or connecting a charger, the system exits Doze and all apps return to normal activity.

At least on the phones that I have tried, in a normal every-day scenario, the notifications are being suspended during either your night sleep, or when the device is sitting there without any interactions/movements, etc.

However, if you really need to deliver every notification and it is crucial to the UX, then you have 2 options:

  1. If you want to have no user interaction - set the priority to high and simply hope that the system will occasionally show some of the notifications (during the maintenance window probably). Not the best way, but by default it may work from time to time.
  2. If you want to guarantee that the notifications will pop-up, then have some screen describing the issue to the user and then navigate the user to Settings -> Apps & notifications --> Advanced --> Special app access --> Battery optimisation. Then he/she can select your app and manually set it to Don't optimise. Keep in mind that this is not considered a best practice and: a) the user shouldn't know about the consequences from this action and b) it is better to leave everything to the OS in order for it to work as intended.

To sum up, you don't have to set your notification's priority to high, because the OS will see that it a simple informative notification and it will now wake the phone. Keep in mind that, when the phone goes into "deep sleep" Doze mode, it shouldn't and mustn't wake for an informative notifications, so there's no point in trying - that's by design. Otherwise, every other developer can set its notification priority to high and the phone may never go to Doze mode, which will make the feature useless.

like image 200
Mihail Duchev Avatar answered Oct 12 '22 09:10

Mihail Duchev