Push notifications

The Zendesk SDK for Android supports Firebase Cloud Messaging for sending and receiving push notifications. The following steps will guide you, or your development team, through the process of setting up push notifications for the Zendesk SDK in your app.

FCM is included as a compile-only dependency of the SDK, which means that out-of-the-box there are no additional steps required if you don't plan on using push notifications.

If you plan to use push notifications in your app, add the dependencies below to your gradle file and implement cloud messaging in your app as described in Set up a Firebase Cloud Messaging client app on Android in the Firebase docs so your app is capable of receiving push notifications from Firebase.

```implementation platform("com.google.firebase:firebase-bom:{latest_version}")implementation "com.google.firebase:firebase-messaging:{latest_version}"```

Step 1 - Setting up push notifications in Firebase

Set up cloud messaging in your app as described in Set up a Firebase Cloud Messaging client app on Android in the Firebase docs so your app is capable of receiving push notifications from Firebase.

Step 2 - Adding your Firebase key and ID in the Zendesk Admin Center

Now that you have set up up cloud messaging in Firebase, your Firebase key and ID will need to uploaded to the Zendesk Admin Center by an admin on your Zendesk account. If you are not an admin, please contact the admin on your account to assist you with this step.

Step 3 - Implementing push notifications in your app's codebase

When implementing push notifications in your app's codebase, you have two options. You can implement a default implementation of FirebaseMessagingService, or use your own if you so choose. The steps to complete both options are detailed below.

Using the default implementation of FirebaseMessagingService

The Zendesk SDK for Android provides a default implementation of FirebaseMessagingService that will:

  • Notify the SDK when a new device token has been created
  • Display the push notification if Messaging was the source of the received message

If the Zendesk SDK for Android is the only expected source of push notifications for your app, then you can register the DefaultMessagingService class in the application tag of your AndroidManifest.xml.

<application>    <!-- Use the SDK default messaging service -->    <service android:name="zendesk.messaging.android.push.DefaultMessagingService"        android:exported="false">        <intent-filter>            <action android:name="com.google.firebase.MESSAGING_EVENT" />        </intent-filter>    </service></application>

Using a custom implementation of FirebaseMessagingService

If you want to use your own FirebaseMessagingService implementation in your app, then the Zendesk SDK for Android provides a set of convenience methods for handling push notifications.

Updating push notification tokens

To enable a device to receive push notifications, you must notify the SDK when a new token has been created.

The snippets below demonstrate how to handle updating the push token.

Kotlin

import com.google.firebase.messaging.FirebaseMessagingServiceimport zendesk.messaging.android.push.PushNotifications
/** * A sample integration of [FirebaseMessagingService] that interacts with Messaging SDK. */class SampleMessagingService : FirebaseMessagingService() {
    override fun onNewToken(newToken: String) {        PushNotifications.updatePushNotificationToken(newToken)    }}

Java

import androidx.annotation.NonNull;import com.google.firebase.messaging.FirebaseMessagingService;import zendesk.messaging.android.push.PushNotifications;
public class SampleMessagingService extends FirebaseMessagingService {
	@Override	public void onNewToken(@NonNull String newToken) {        PushNotifications.updatePushNotificationToken(newToken);	}}
Displaying a push notification from Messaging

When a push notification is received, you must determine if it belongs to the SDK. You can then give the data to the SDK to display the notification on your behalf.

The snippets below demonstrate how to integrate and display the push notification.

Kotlin

import com.google.firebase.messaging.FirebaseMessagingServiceimport com.google.firebase.messaging.RemoteMessageimport zendesk.messaging.android.push.PushNotificationsimport zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_DISPLAYimport zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_NOT_DISPLAYimport zendesk.messaging.android.push.PushResponsibility.NOT_FROM_MESSAGING
/** * A sample integration of [FirebaseMessagingService] that interacts with Messaging SDK. */class SampleMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {    	val responsibility = PushNotifications.shouldBeDisplayed(remoteMessage.data)        when (responsibility) {            MESSAGING_SHOULD_DISPLAY -> {            	// This push belongs to Messaging and the SDK is able to display it to the end user                PushNotifications.displayNotification(context = this, messageData = remoteMessage.data)            }            MESSAGING_SHOULD_NOT_DISPLAY -> {                // This push belongs to Messaging but it should not be displayed to the end user            }            NOT_FROM_MESSAGING -> {                // This push does not belong to Messaging            }        }    }}

Java

import androidx.annotation.NonNull;import com.google.firebase.messaging.FirebaseMessagingService;import com.google.firebase.messaging.RemoteMessage;import zendesk.messaging.android.push.PushNotifications;import zendesk.messaging.android.push.PushResponsibility;
public class SampleMessagingService extends FirebaseMessagingService {
	@Override	public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {		PushResponsibility responsibility = PushNotifications.shouldBeDisplayed(remoteMessage.getData());		switch (responsibility) {			case MESSAGING_SHOULD_DISPLAY:            	// This push belongs to Messaging and the SDK is able to display it to the end user                PushNotifications.displayNotification(this, remoteMessage.getData());                break;            case MESSAGING_SHOULD_NOT_DISPLAY:            	// This push belongs to Messaging but it should not be displayed to the end user            	break;            case NOT_FROM_MESSAGING:                // This push does not belong to Messaging                break;		}	}}

Note: When the app is in a background state, PushNotifications.displayNotification will bring you back to the conversation. When the app is in a killed state however, it will just open the app as the SDK is not initialized then. If you call this method with a notification that is not from Zendesk, it won't be displayed.

If you want to change that behaviour, you will have to implement your own handling of the push display as well as handling the initialization of the SDK and call .showMessaging to display the conversation.

(Optional) Step 4 - Changing the notification small icon

The Zendesk SDK for Android uses a chat bubble icon when displaying notifications from Messaging. This icon can be customized by providing a resource ID to your own icon.

The snippets below demonstrate how to provide a custom icon ID.

Kotlin

PushNotifications.setNotificationSmallIconId(R.drawable.my_icon)

Java

PushNotifications.setNotificationSmallIconId(R.drawable.my_icon);

Demo App

You can find a demo app showcasing this feature in our Zendesk SDK Demo app github.