Skip to content

Responding to Notification

levching edited this page Apr 14, 2020 · 4 revisions

When your app is not running or is in the background, the system automatically delivers local notifications. If your app is running in the foreground, notifications are delivered directly to your app. You can then decide whether to handle the notification quietly or alert the user. To respond to the delivery of notifications, you must subscribe to the OnNotificationReceived event. See the example below.

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.OnNotificationReceived.AddListener((UM_NotificationRequest request) => {
    //Notification was received while app is running
});

You may also react is user clicked on the notification when your app was in the background mode. When an app is switched from background to foreground, you can act accordingly. For example, if a user clicked on a "reminder" notification to get his daily reward, it's a good practice to bring daily reward screen as soon as your application is switched to the foreground. To respond to the notification click, you must subscribe to the OnNotificationClick event. See the example below.

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.OnNotificationClick.AddListener((UM_NotificationRequest request) => {
     //User clicked on notification while app is running
});

See the next chapter how to respond if your app was launched using notification.

Handling Notifications When Your App Is in not running

If the user clicked on notification generated by your app, the application will be launched. There is a way to find out for you if your app was launched by notification click. You can use LastOpenedNotificationRequest as soon as the application is started. If LastOpenedNotificationt contains the UM_NotificationRequest object, means your app was launched with a user clicking the notification. So you can act accordingly.

using SA.CrossPlatform.Notifications;
...

UM_NotificationRequest startRequest = client.LastOpenedNotificationt;
if(startRequest != null) {
    //if this isn't null on your app launch, means user launched your app by clicking on notification icon
}

Canceling Notifications

It's good practice to cancel all pending notifications once your app is launched and re-schedule new ones. By implementing this little trick, you can make sure that the user will not get notifications while your application running. There are several methods you can operate.

If you need to remove already delivered notification. You may use the RemoveDeliveredNotification(int Identifier) method for that purpose:

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.RemoveDeliveredNotification(100);

Or you can remove all of the delivered notifications with the RemoveAllDeliveredNotifications method.

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.RemoveAllDeliveredNotifications();

When you need to unschedule notification that wasn't yet delivered, use RemovePendingNotification(int Identifier) method

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.RemovePendingNotification(100);

Or you can remove all of the pending notifications with the RemoveAllPendingNotifications method.

using SA.CrossPlatform.Notifications;
...

var client = UM_NotificationCenter.Client;
client.RemoveAllPendingNotifications();
Clone this wiki locally