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 the Firebase Cloud Messaging server key for Android.

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

Getting the FCM server key

  1. In Firebase, click the project or app and go to Settings.
  2. Click the Cloud Messaging tab.
  3. Copy the server key listed, or add a server key.

Adding the server key to Chat

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

Note: If your app is targeting Android O and above, make sure the app properly handles the notification channel for these push notifications.

After the Chat admin confirms they've uploaded the server key to the account, enable push notifications in your app as described in the next section.

Enabling push notifications in your app

You need to register devices interested in receiving push notifications with Zendesk Chat.

First, go through the following guide from Google on implementing an FCM client on Android: Implementing an FCM Client on Android. After initializing Firebase you will be able to access the device registration token.

Second, send the token to the Zendesk Chat servers through the Chat SDK as follows to register the device:

ZopimChat.setPushToken(token);

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)

{
  "data": {
    "author" : "{agent name}",
    "type" : "zd.chat.msg",
    "ts" : {millisecond timestamp (long)}
  }
}

Chat ended (zd.chat.end)

{
  "data": {
    "author" : "{agent name}",
    "type" : "zd.chat.end",
    "ts" : {millisecond timestamp (long)}
  }
}

Processing push notifications in your app

You can use the PushData.getType() method to identify the push notification type. Based on the result, you can notify the SDK when a message is received.

final PushData pushData = PushData.getChatNotification(message.getData());

switch(pushData.getType()) {
    case END:
        // present the notification as desired
        break;
    case MESSAGE:
        // present the notification as desired
        break;
    case NOT_CHAT:
        // ignore
        break;
}

// IMPORTANT! forward the notification data to the SDK
ZopimChatApi.onMessageReceived(pushData);

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 'AgentMessage' chat log event. See Chat log events for more information.