Push Notifications

You can use push notifications with the Chat SDK to notify your users every time the agent sends a new message to the conversation.

Note: If your account is using Agent Workspace, you will not receive a notification for the end of the Chat session. This is due to the termination of the session being different. We recommend your agent send a message before closing the chat to avoid uninformed termination.

Setting up the push notifications in Zendesk Chat dashboard

To set up push notifications in your Chat dashboard, follow the guidelines in Zendesk help. After enabling push notifications, integrators will need the chat account key and the appID. These are passed into the chat initialization code:

Swift

import ChatSDKimport ChatProvidersSDK
Chat.initialize(accountKey: <#"Your account key"#>, appId: <#Your app identifier#>)

Objective-C

#import <ChatSDK/ChatSDK.h>#import <ChatProvidersSDK/ChatProvidersSDK.h>
[ZDKChat initializeWithAccountKey:<#"Your account key"#> appId:<#Your app identifier#> queue:dispatch_get_main_queue()];

Configuring Apple Push Notification Service

For instruction on how to generate a .pem file for Apple's Push Notification Service, see Enabling iOS push notifications for the Chat SDK.

Enabling push notifications when the app is in background mode

This section outlines the steps to enable push notifications. Push notifications are sent whenever the app is in background mode and the chat is in a disconnected state. If you are using the out-of-the-box UI, chat is disconnected whenever the user is not in the chat view. For in-app notifications, see Enabling in-app push notifications in your app.

1. Request the authorization for push notifications from your user

Register for push notifications using UNUserNotificationCenter in your AppDelegate (only for the device). See Setting up in-app push notifications for Chat SDK below to implement UNUserNotificationCenter.

Swift

import UserNotifications
#if !TARGET_IPHONE_SIMULATORlet notificationCenter = UNUserNotificationCenter.current()notificationCenter.delegate = selfnotificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in   guard granted else { return }
   DispatchQueue.main.async {      UIApplication.shared.registerForRemoteNotifications()   }}#endif

Objective-C

#import <UserNotifications/UserNotifications.h>#import <ChatProvidersSDK/ChatProvidersSDK.h>
#if !TARGET_IPHONE_SIMULATORUNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];center.delegate = self;[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge)   completionHandler:^(BOOL granted, NSError * _Nullable error){  if (!error){      [[UIApplication sharedApplication] registerForRemoteNotifications];  }}];#endif
2. Register the received device token in the Chat SDK

Override the func application (_application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) function in your AppDelegate.

Swift

import ChatProvidersSDK
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {    Chat.registerPushToken(deviceToken)}

Objective-C

#import <ChatProvidersSDK/ChatProvidersSDK.h>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {   [ZDKChat registerPushToken:deviceToken];}

3. Pass the received remote push notification to the Chat SDK

Swift

import ChatProvidersSDK
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {   let userInfo = response.notification.request.content.userInfo   let application = UIApplication.shared   Chat.didReceiveRemoteNotification(userInfo, in: application)   completionHandler()}

Objective-C

#import <ChatProvidersSDK/ChatProvidersSDK.h>
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{   [ZDKChat didReceiveRemoteNotification:userInfo in:application];   completionHandler();}

Enabling in-app push notifications in your app

Allow the notifications to be shown in your app by implementing the UNUserNotificationCenterDelegate method.

Swift

import UserNotifications
func userNotificationCenter(_ center: UNUserNotificationCenter,                           willPresent notification: UNNotification,                           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {   completionHandler([.alert, .sound, .badge])}

Objective-C

#import <UserNotifications/UserNotifications.h>
// In AppDelegate.h conform AppDelegate to UNUserNotificationCenterDelegate@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>- (void)userNotificationCenter:(UNUserNotificationCenter *)center          willPresentNotification:(UNNotification *)notification        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {           completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);}

Types of push notifications

The Chat SDK supports two types of push notifications:

  • New chat message notification
  • Chat has ended notification
New chat message notification
{  "aps" : {    "alert" : {      "title" : "{agent name}",      "body" : "{agent message}",    }  },  "data": {    "type" : "zd.chat.msg",    "ts" : "{millisecond timestamp (long)}"  }}
Chat has ended notification
{  "aps" : {    "alert" : {      "title-loc-key" : "ios.ZDCChat.pushChatEndedTitle",      "loc-key" : "ios.ZDCChat.pushChatEndedBody",      "loc-args" : [ "{agent name}" ]    }  },  "data": {    "type" : "zd.chat.end",    "ts" : "{millisecond timestamp (long)}"  }}

Note: If your account is using Agent Workspace, you will not receive a notification for the end of the Chat session. This is due to the termination of the session being different. We recommend your agent send a message before closing the chat to avoid uninformed termination.

Identifying the received push notification

Chat SDK allows you to identify the push notifications sent by Zendesk by leveraging the PushNotificationProvider.

Identifying the Chat SDK push notification

Swift

import ChatProvidersSDK
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {   ...
   if Chat.pushNotificationProvider?.isChatPushNotification(userInfo) {     // handle push notification sent by Chat   }
   ...}

Objective-C

#import <ChatProvidersSDK/ChatProvidersSDK.h>
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{   ...
   if ([ZDKChat.pushNotificationsProvider isChatPushNotification:userInfo]) {     // handle push notification sent by Chat   }
   ...}

Identifying the type of the push notification

Additionally you can leverage the type-safe PushNotificationData class to reason about the type of the push notification.

Swift

import ChatProvidersSDK
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {   ...
   let pushNotificationData = PushNotificationData.data(for: userInfo)   switch pushNotificationData?.type {     case .message:         // message notification was received     case .chatEnded:        // chat ended notification was received   }
   ...}

Objective-C

#import <ChatProvidersSDK/ChatProvidersSDK.h>
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{   ...
   ZDKPushNotificationData *data = [ZDKPushNotificationData dataFor:userInfo];   if (data != nil) {      switch (data.type) {        case ZDKPushNotificationTypeMessage:            // message notification was received            break;        case ZDKPushNotificationTypeChatEnded:            // chat ended notification was received            break;;        default:            break;      }   }
   ...}

Localizing push notifications

To learn how to localize push notifications for Chat SDK, see the Unified SDK documentation.