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.

Setting up the push notifications in Zendesk Chat dashboard

To set up push notifications in your dashboard, follow the guidelines in the Chat Help Center.

Configuring Apple Push Notification Service

For instruction on how to generate a .pem file for Apple's Push Notification Service, follow the documentation here in the Develop Help Center.

Enabling push notifications in your app

This section outlines the steps to enable push notifications in your iOS application.

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)}"  }}

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.