Invalidating and switching SDK instances
Invalidate the SDK
Invalidating the Zendesk SDK refers to the process of stopping its current instance. Upon instance invalidation, Zendesk SDK nullifies the invalidated instance and references to the internal storages are lost. This ensures that unused data does not accumulate over time, freeing up system resources. When the end user logs out, the Zendesk SDK removes all user details from local storages and terminates the real-time connection. Invalidating the Zendesk SDK instance means that no messages or notifications will be received.
Switching between instances
The Zendesk SDK enables switching between different instances at runtime, which is useful for running multiple instances within a single application.
Push notifications for switching between instances
No additional setup is required to switch between instances. However, an API is available to validate incoming push notifications for the currently active instance.
To handle push notifications for switching instances, add the following API to your custom implementation of the FirebaseMessagingService
:
PushNotifications.validatePushIntegration(context: Context, messageData: Map<String, String>)
This API will need to be called in the onMessageReceived
method when a push notification will be displayed.
Kotlin
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import zendesk.messaging.android.push.PushNotifications
import zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_DISPLAY
import zendesk.messaging.android.push.PushResponsibility.MESSAGING_SHOULD_NOT_DISPLAY
import 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 -> {
// The push needs to be validated that it belongs to the current SDK instance.
if (PushNotifications.validatePushIntegration(this, remoteMessage.data)) {
// This push has been validated and it belongs to Messaging so the SDK is able to display it to the end user
PushNotifications.displayNotification(
context = this,
messageData = remoteMessage.data,
)
} else {
// This push does not belong to the current SDK instance and should not be displayed.
}
}
}
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:
// The push needs to be validated that it belongs to the current SDK instance.
if (PushNotifications.validatePushIntegration(this, remoteMessage.getData())) {
// This push has been validated and it belongs to Messaging so the SDK is able to display it to the end user
PushNotifications.displayNotification(this, remoteMessage.getData());
} else {
// This push does not belong to the current SDK instance and should not be displayed.
}
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;
}
}
}