Push notifications in Kit Chat for iOS
Voximplant Kit sends alert push notifications about new inbound chat messages. Notifications are delivered through APNs and displayed automatically by the system. The application does not need to create notifications manually.
This article describes how to register a push token, configure notification handling in an active application, change notification content, and test notifications.
Before you start, configure push notifications in Voximplant Kit as described in the guide.
Contents
Push notification structure
Example of a payload received from Voximplant Kit:
aps— system APNs block required to receive and display an alert push notification with the default sound. Themutable-content: 1field allows you to change the notification through Notification Service Extension before it is displayed.
voximplant_im— data block from Voximplant Kit:client_id— ID of the client the push notification is intended formessage_uuid— message UUIDwith_attachments— attachment availability flag
Push token registration
To let the application receive notifications, request the user's permission to display notifications and register the APNs token in Voximplant Kit.
Requesting permission and getting a token
Registering a token in the SDK
The isDevelopment parameter value depends on the application build type:
true— Xcode build in debug mode, token received from sandbox APNsfalse— build for App Store or TestFlight, token received from production APNs
To stop receiving notifications, use VIKitChatUI.unregisterPushToken(_:isDevelopment:completion:) in the same way.
Notification handling
Managing notifications in an active application
When the application is active, iOS does not display push notifications by default if the UNUserNotificationCenterDelegate delegate is not assigned or the userNotificationCenter(_:willPresent:withCompletionHandler:) method is not implemented.
If notifications are needed in an active application, but not on all screens, assign the UNUserNotificationCenter delegate and define the logic in the userNotificationCenter(_:willPresent:withCompletionHandler:) method.
In the example below, notifications are displayed in all cases except when VIKitChatViewController is displayed on the screen:
The push notification payload from Voximplant Kit contains the voximplant_im key. You can use this key for filtering if the application receives push notifications from different sources.
Notification customization
Notification customization is performed through Notification Service Extension. The extension receives a push notification before iOS shows the notification to the user and lets you change the displayed title, text, sound, badge, or other parameters.
Available customization options:
Changing the title and text
Localization
Sound management
Badge management
Using data from the push notification payload
Running asynchronous code
To set up notification customization, create a separate target in Xcode: File > New > Target > Notification Service Extension. Then add the required customization logic to this extension.
Notification Service Extension limitations:
Approximately 30 seconds for processing. After that, UNNotificationServiceExtension.serviceExtensionTimeWillExpire() is called
Approximately 24 MB of memory
A separate process. Use App Group to share data with the application
If code execution in Notification Service Extension ends with return without calling contentHandler, the notification will not be displayed.
If Notification Service Extension already contains checks for other push notification types, make sure that push notifications from Voximplant Kit with the
voximplant_imkey in the payload pass these checks and thatcontentHandleris called for them. Otherwise, the notification will not be displayed to the user.
In the standard case, Notification Service Extension receives a push notification from Voximplant Kit and changes the notification content, for example, replaces the text:
Other customization options
Localization
To localize notification text, use NSLocalizedString. It returns a localized value from the .strings file of the Notification Service Extension target based on the device language:
Disabling sound
By default, the push notification from Voximplant Kit specifies a sound. To show the notification without sound, use the following code:
When Do Not Disturb mode is enabled, iOS mutes notification sounds regardless of this setting.
Badge management
Badge is a numeric counter on the application icon. To work with badge between Notification Service Extension and the application, a shared App Group is required:
Using payload data
Data from the push notification payload can be used to change the notification the user will see. In the example below, if attachments are present (with_attachments: 1), an icon is added to the notification:
Running asynchronous code
Notification Service Extension supports asynchronous code execution, for example, network requests. In the example below, an image is downloaded to be displayed in the notification:
In this implementation example, if the request is not completed within approximately 30 seconds, serviceExtensionTimeWillExpire() passes bestAttemptContent with unmodified content. The user will see the notification without the image.
Push notification testing
Before integration, we recommend testing push notifications using Apple Push Notifications Console. For details, see the documentation. The console lets you send an APNs push with a specified payload to a device using its token.
This helps check the application behavior with the current codebase when new push notifications are received and understand whether changes are required.
For testing, you can use the following payload. If necessary, replace the values with test data:
Migration from previous versions
In SDK versions below 1.6.0, SDK APIs were used for notification customization. Starting with version 1.6.0, these APIs are deprecated.
When updating, do the following:
Remove calls to
configureChatNotification(configuration:)andhandlePushNotification(_:completion:).Remove the use of
VINotificationConfiguration.Remove chat-related push notification handling in application(_:didReceiveRemoteNotification:fetchCompletionHandler:).
Replace
VIPushNotificationErrorwith VIPushTokenError.For notification customization, use Notification Service Extension as described above.