Push notifications

You can use push notifications to notify the visitor when the app is in the background.

Push notifications are handled by Zendesk server-side. However, you still need to provide a combined PEM file for iOS APN support.

Note: Push notification support is only available on paid Chat plans. It's not available on the Lite plan.

Generating a combined PEM file

To generate the combined PEM file, export the APNS SSL certificate and the private key from Keychain Access on your Mac computer.

When exporting, don't enter a password when asked about protecting the export.

If you don't know how to combine the certificate PEM and server key PEM, use the following command in Terminal:

cat PushAppCert.pem PushAppKey.pem > PushAppCertAndKey.pem

Then test the combined PEM file against the sandbox Apple server using the following command:

telnet gateway.sandbox.push.apple.com 2195

Finally, test the connection using the SSL certificate and private key to set up a secure connection:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem

Uploading the combined PEM file to Chat

You must be a Chat admin to upload the PEM file. If you're not a Chat admin, ask one to do it for you and provide them with the PEM file. For the admin instructions, see Enabling push notifications for the Chat SDK for mobile in the Chat Help Center.

Make sure you use the proper certificate and key in the PEM file for the profile you're using. For example, don't use a combined PEM file for staging in the production profile of the app.

After the Chat admin confirms they've uploaded the PEM file to the account successfully, enable push notifications as described in the next section.

Enabling push notifications in your app

Make the following updates to the AppDelegate file.

  1. On push registration, send the token to the SDK:

    Objective-C

    - (void) application:(UIApplication*)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)tokenData {
        [ZDCChat setPushToken:tokenData];
    }
    

    Swift

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken tokenData: Data) {
        ZDCChat.setPushToken(tokenData)
    }
    
  2. On receipt of a push notification:

    Objective-C

    - (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
        [ZDCChat didReceiveRemoteNotification:userInfo];
    }
    

    Swift

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        ZDCChat.didReceiveRemoteNotification(userInfo)
    }
    

Push notification types

The SDK provides two types of push notifications:

  • Chat message notifications (zd.chat.msg)
  • Chat ended notifications (zd.chat.end)

The payload for each type of notification is as follows:

Chat message (zd.chat.msg)

{
  "aps" : {
    "alert" : {
      "title" : "{agent name}",
      "body" : "{agent message}",
    }
  },
  "data": {
    "type" : "zd.chat.msg",
    "ts" : "{millisecond timestamp (long)}"
  }
}

Chat ended (zd.chat.end)

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

Localizing push notifications

Push notification strings are localized the same way as other strings. See Strings and localization.

Handling local notifications

If you want to render notifications when the app is in the foreground, handle it separately by listening to the 'ZDCChatEventTypeAgentMessage' chat log event. See Events for more information about chat log events.