Skip to main content

Push notifications

About this documentation

This documentation only describes how to configure the SDK for our push notifications feature. If you want to learn about the feature and the full set of implementation requirements, see the main feature documentation:


How do push notifications work?

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

Configure the SDK

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: [.list, .banner]) { granted, error in }
    application.registerForRemoteNotifications()
    ...
    }
  3. When the registration succeeds, the UIApplicationDelegate method didRegisterForRemoteNotificationsWithDeviceToken will be invoked.
  4. Set the deviceToken property on your EncapPush instance:
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    self.encapPush.deviceToken = deviceToken
    }
  5. For every new notification sent to the application, the UNUserNotificationCenterDelegate methods will be invoked:
    MethodDescription
    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.
  6. Notify the SDK by invoking handleNotificationWithUserInfo on your EncapPush instance:
    let purpose = self.encapPush.handleNotification(userInfo)
  7. When the SDK receives the notification, it will:
    1. Interpret the notification.
    2. Establish the type of operation it requests of your application.
    3. Return what type of purpose the push has.
  8. This allows your application to present the correct user interface to the end-user:
    if let purpose = self.encapPush.handleNotification(notification.request.content.userInfo) {
    switch purpose {
    case .authentication:
    print("A notification has arrived requesting the application to authenticate the user. Invoke startAuthentication and present your authentication user interface.")
    case .performRecovery:
    print("Account Recovery")
    }
    } else {
    print("This push is not from Encap")
    }

Customise the push message

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

To do this, you need to add the supported keys in the app's Localizable.strings resource file. You can use the following supported keys:

  • notification.authenticate
  • notification.performRecoveryNotification
Example: Localizable.strings for authenticate
"notification.authenticate" = "New authorization request received";

Customise the 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.