Recent searches


No recent searches

`hideMessaging()` to hide chat in messaging SDK



Posted Apr 29, 2024

Our Android app has several events that require us to log out users and close all activities.

E.g. when our  access token expires, we need to log out user and show the login page per our InfoSec requirements.

 

Zendesk SDK has API to start messaging via showMessaging(), function but we can't find an option to hide the messaging chat. Can you guide us, what we can do here? 


6

4

4 comments

We also have the same issue and would appreciate to get an answer too

0


image avatar

Erica Girges

Zendesk Developer Advocacy

Hi Anton and Nika!

For any UI customization outside of what's currently available, you can implement yourself by using the API providers.  It's like using all of the functionality of the Zendesk SDK just minus our UI. 

 

Hope this helps!

0


Please support this.

0


Got it to work by using a hacking workaround. I noticed in the logs that a new Activity for zendesk.messaging.android.internal.conversationscreen.ConversationActivity is created when showMessaging is called, so I catch that Activity on the MainApplication and switch it with the current activity so I can finish it later.

Step-by-Step Solution

 

Step 1: Modify the Application Class

 

Create a global variable to track the ConversationActivity and use ActivityLifecycleCallbacks to monitor its lifecycle events.

 

MyApplication.java

package com.example.app;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
public class MyApplication extends Application {
   private static MyApplication instance;
   private Activity currentActivity;
   @Override
   public void onCreate() {
       super.onCreate();
       instance = this;
       registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
           @Override
           public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
               if (activity.getClass().getName().equals("zendesk.messaging.android.internal.conversationscreen.ConversationActivity")) {
                   currentActivity = activity;
               }
           }
           @Override
           public void onActivityStarted(Activity activity) {
               if (activity.getClass().getName().equals("zendesk.messaging.android.internal.conversationscreen.ConversationActivity")) {
                   currentActivity = activity;
               }
           }
           @Override
           public void onActivityResumed(Activity activity) {
               if (activity.getClass().getName().equals("zendesk.messaging.android.internal.conversationscreen.ConversationActivity")) {
                   currentActivity = activity;
               }
           }
           @Override
           public void onActivityPaused(Activity activity) {}
           @Override
           public void onActivityStopped(Activity activity) {}
           @Override
           public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
           @Override
           public void onActivityDestroyed(Activity activity) {
               if (activity == currentActivity) {
                   currentActivity = null;
               }
           }
       });
   }
   public static MyApplication getInstance() {
       return instance;
   }
   public Activity getCurrentActivity() {
       return currentActivity;
   }
}

 

 

Step 2: Create a Utility Class to Manage Zendesk Messaging

 

Create a utility class that interacts with the application class to show and hide the Zendesk messaging.

 

ZendeskManager.java

package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import zendesk.android.Zendesk;
import zendesk.messaging.android.DefaultMessagingFactory;
import zendesk.messaging.android.Messaging;
import zendesk.messaging.android.MessagingDelegate;
import zendesk.messaging.android.UrlSource;
public class ZendeskManager {
   private static final String TAG = "ZendeskManager";
   public static void initializeZendesk(Context context, String zendeskKey) {
       Zendesk.initialize(
           context,
           zendeskKey,
           zendesk -> {
				// Your code
           },
           error -> Log.e(TAG, "Messaging failed to initialize", error),
           new DefaultMessagingFactory());
   }
   public static void showMessaging(Context context) {
       Activity currentActivity = MyApplication.getInstance().getCurrentActivity();
       if (currentActivity != null) {
           Log.d(TAG, "Showing Zendesk messaging");
           currentActivity.runOnUiThread(() -> Zendesk.getInstance().getMessaging().showMessaging(currentActivity));
       } else {
           Log.e(TAG, "No current activity to show messaging");
       }
   }
   public static void hideMessaging() {
       Activity currentActivity = MyApplication.getInstance().getCurrentActivity();
       if (currentActivity != null) {
           Log.d(TAG, "Finishing ConversationActivity");
           currentActivity.runOnUiThread(() -> currentActivity.finish());
       } else {
           Log.e(TAG, "No ConversationActivity to finish");
       }
   }
}

 

Hope it helps!

 

PS: Zendesk please add support for easily hideMessaging the same way as we do for showMessaging 

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post