Push notifications
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:
- Create an instance of
EncapPush. - 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()
...
} - When the registration succeeds, the
UIApplicationDelegatemethoddidRegisterForRemoteNotificationsWithDeviceTokenwill be invoked. - Set the
deviceTokenproperty on yourEncapPushinstance:func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
self.encapPush.deviceToken = deviceToken
} - For every new notification sent to the application, the
UNUserNotificationCenterDelegatemethods 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 SDK by invoking
handleNotificationWithUserInfoon yourEncapPushinstance: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.
- 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.authenticatenotification.performRecoveryNotification
"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:
- Select your project in Xcode.
- Select Signing & Capabilities, then add Time Sensitive Notifications.
- In your application configuration, set
APNS_TIME_SENSITIVE_INTERRUPTION_LEVEL_ENABLEDtotrue.