Rate this page:

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.

Information

Before you start, configure push notifications in Voximplant Kit as described in the guide.

Contents

Copy URL

Push notification structure

Copy URL

Example of a payload received from Voximplant Kit:

Push notification payload example
  • aps — system APNs block required to receive and display an alert push notification with the default sound. The mutable-content: 1 field 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 for
    • message_uuid — message UUID
    • with_attachments — attachment availability flag

Push token registration

Copy URL

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

Request permission and get a token

Registering a token in the SDK

Register a push token

The isDevelopment parameter value depends on the application build type:

  • true — Xcode build in debug mode, token received from sandbox APNs

  • false — 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

Copy URL

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:

Handle notifications in an active application

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

Copy URL

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:

Note

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_im key in the payload pass these checks and that contentHandler is 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:

Customize notification text

Other customization options

Copy URL

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:

Localize notification text

Disabling sound

By default, the push notification from Voximplant Kit specifies a sound. To show the notification without sound, use the following code:

Disable notification sound
Information

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:

Manage badge

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:

Use payload data

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:

Run async code

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

Copy URL

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:

Payload example for push notification testing

Migration from previous versions

Copy URL

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:

  1. Remove calls to configureChatNotification(configuration:) and handlePushNotification(_:completion:).

  2. Remove the use of VINotificationConfiguration.

  3. Remove chat-related push notification handling in application(_:didReceiveRemoteNotification:fetchCompletionHandler:).

  4. Replace VIPushNotificationError with VIPushTokenError.

  5. For notification customization, use Notification Service Extension as described above.

Rate this page: