# Push notification

# Overview

We support the use of the Apple Push Notification service (APNs) to notify the end-user when an operation has started.

# Configure push

To add support for notifications to your application, in the UIApplicationDelegate method didFinishLaunchingWithOptions, you have to:

  1. Create an instance of EncapPush.
  2. Register for APNS notifications.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
    self.encapPush = EncapPush()       
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in }
    application.registerForRemoteNotifications()
    ...
}

When the registration succeeds, the UIApplicationDelegate method didRegisterForRemoteNotificationsWithDeviceToken will be invoked.

Set the deviceToken property on your EncapPush instance.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    self.encapPush.deviceToken = deviceToken
}

For every new notification sent to the application, the UNUserNotificationCenterDelegate methods will be invoked:

Method Description
userNotificationCenter:willPresentNotification:withCompletionHandler: If the application is in the foreground, this method is called when a notification is about to be presented to the user.
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: If the application is in the background and the user has responded to the notification by opening the application, this method is called.

Notify the EncapAPI by invoking handleNotificationWithUserInfo on your EncapPush instance:

let purpose = self.encapPush.handleNotification(userInfo)

When the SDK receives the notification, it will:

  • Interpret the notification.
  • Establish the type of operation it requests of your application.
  • Return what type of purpose the push has. purpose.

This allows your application to present the correct user interface to the end-user.

Note

encapPush.handleNotification(userInfo) will not trigger any delegate methods in EncapPushDelegate.

These delegate methods are deprecated and will be removed.

# Customise push message

Encap sets the loc-key in the push payload so that the app developer can customise the message in the push notification.

This is done by adding the supported keys in the app's Localizable.strings resource file.

The supported keys that can be used are:

  • notification.authenticate
  • notification.performRecoveryNotification

Example from Localizable.strings for authenticate:

"notification.authenticate" = "New authorization request received";

# Custom push payload

You can add a custom string in the push payload from the server-side.

The customPushPayload method can be used to parse the value. If no value is present, then nil will be returned.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let customPushPayload = self.encapPush.customPushPayload(notification.request.content.userInfo)
    completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let customPushPayload = self.encapPush.customPushPayload(response.notification.request.content.userInfo)
    completionHandler()
}

# Time-Sensitive notifications

Time Sensitive notifications are notifications that can break through system controls such as Notification Summary and Focus.

We recommend that you use them to ensure that the end-user can see the requests, even when in Focus mode.

To add support for these push notifications:

  1. Select your project in Xcode.
  2. Select Signing & Capabilities, then add Time Sensitive Notifications.
  3. In your application configuration, set APNS_TIME_SENSITIVE_INTERRUPTION_LEVEL_ENABLED to true.
Last updated: 04/03/2024 15:17 UTC