Skip to content

Responding to Notification

levching edited this page Apr 15, 2020 · 5 revisions

When your app is not running or is in the background, the system automatically delivers local and remote notifications using the interactions you specified. 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 ISN_UNUserNotificationCenterDelegate events.

WillPresentNotification event called when a notification is delivered to a foreground app. If your app is in the foreground when a notification arrives, the notification center calls this method to deliver the notification directly to your app. If you implement this method, you can take whatever actions are necessary to process the notification and update your app. A user will not be alerted by a system.

using SA.iOS.UserNotifications;
...
ISN_UNUserNotificationCenterDelegate.WillPresentNotification.AddListener((ISN_UNNotification notification) => {
    PrintNotification(notification);
});

private void PrintNotification(ISN_UNNotification notification) {
    Debug.Log("notification.Date: " + notification.Date.ToString());

    Debug.Log("notification.Request.Identifier: " + notification.Request.Identifier);

    Debug.Log("notification.Request.Content.Title: " + notification.Request.Content.Title);
    Debug.Log("notification.Request.Content.Subtitle: " + notification.Request.Content.Subtitle);
    Debug.Log("notification.Request.Content.Body: " + notification.Request.Content.Body);
    Debug.Log("notification.Request.Content.Sound: " + notification.Request.Content.Sound);

    Debug.Log("notification.Request.Trigger.Type: " + notification.Request.Trigger.Type);
}

The notification will be represented as the ISN_UNNotification object

The DidReceiveNotificationResponse Called to let your app know which action was selected by the user for a given notification. When the user responds to a notification, the system calls this method with the results. For example when a user clicks on a notification. See the example below:

ISN_UNUserNotificationCenterDelegate.DidReceiveNotificationResponse.AddListener((ISN_UNNotificationResponse resp) => {
    Debug.Log("resp.ActionIdentifier: " + resp.ActionIdentifier);
    PrintNotification(resp.Notification);
});

private void PrintNotification(ISN_UNNotification notification) {
    Debug.Log("notification.Date: " + notification.Date.ToString());

    Debug.Log("notification.Request.Identifier: " + notification.Request.Identifier);

    Debug.Log("notification.Request.Content.Title: " + notification.Request.Content.Title);
    Debug.Log("notification.Request.Content.Subtitle: " + notification.Request.Content.Subtitle);
    Debug.Log("notification.Request.Content.Body: " + notification.Request.Content.Body);
    Debug.Log("notification.Request.Content.Sound: " + notification.Request.Content.Sound);

    Debug.Log("notification.Request.Trigger.Type: " + notification.Request.Trigger.Type);
}

As you may notice the event holds the ISN_UNNotificationResponse object that not only holds an ISN_UNNotification object but also providing you with the action identifier. ActionIdentifier is the identifier for the action that the user selected. This parameter may contain one the identifier of one of your ISN_UNNotificationAction objects or it may contain a system-defined identifier. The system defined identifiers are ISN_UNNotificationAction.DefaultActionIdentifier and ISN_UNNotificationAction.DismissActionIdentifier, which indicate that the user opened the app or dismissed the notification without any further actions.

The DefaultActionIdentifier indicates the user opened the app from the notification interface. The delivery of this action does not require any special configuration of notification categories.

The DismissActionIdentifier indicates the user explicitly dismissed the notification interface. To trigger this action, the user must explicitly dismiss the notification interface. For example, the user must tap the Dismiss button or swipe down on the notification interface in watchOS to trigger this action. This action is delivered only if the notification’s category object was configured with the UNNotificationCategoryOptionCustomDismissAction option. Currently not supported by a plugin.

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 should LastReceivedResponse property of ISN_UNUserNotificationCenterDelegate, as soon as the application is started. If LastReceivedResponse contains the ISN_UNNotificationResponse object, means your app was launched with a user clicking the notification. So you can act accordingly.

using SA.iOS.UserNotifications;
...
var notificationResponse = ISN_UNUserNotificationCenterDelegate.LastReceivedResponse;

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally