Skip to content

Observe Chat Events

Kristiyan Petrov edited this page Mar 23, 2020 · 8 revisions

Observe chat events

To observe a chat that has started, has ended or has ended with an error is it necessary to add a chat UI observer to the chat module.

ChatUIObserver chatUIObserver = new ChatUIObserver() {

    @Override
    public void onActivityStarted(@NonNull Chat chat,
                                  @NonNull WeakReference<AppCompatActivity> activity) {
         Log.d("Bandyer SDK", "onChatActivityStarted");
    }
        
    @Override
    public void onActivityDestroyed(@NonNull Chat chat,
                                    @NonNull WeakReference<AppCompatActivity> activity) {
         Log.d("Bandyer SDK", "onChatActivityDestroyed");
    }

    @Override
    public void onActivityError(@NonNull Chat chat,
                                @NonNull WeakReference<AppCompatActivity> activity,
                                @NonNull ChatException error) {
         Log.e("Bandyer SDK", "onChatActivityError " + error.getMessage());
    }
};

ChatModule chatModule = BandyerSDKClient.getInstance().getChatModule();
if (chatModule == null) return;
chatModule.addChatUIObserver(chatUIObserver);
// or chatModule.addChatUIObserver(fragmentActivity, chatObserver); to automatically dispose the observer on activity destroy.     

Observe Chat Events via Broadcast Receiver

If you need to decouple the logic of observing a chat it is also provided a way to observe chat events through a broadcast receiver as shown below:

Register the chat event broadcast receiver in AndroidManifest.xml as shown below:

<!-- Bandyer call event broadcast receiver -->

<application>

        <!-- Bandyer chat event broadcast receiver -->

        <receiver
            android:name=".ChatEventBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.bandyer.android_sdk.CHAT_EVENT_ACTION" />
            </intent-filter>
        </receiver>


</application>

Add the chat event broadcast receiver class to your project:

public class ChatEventBroadcastReceiver extends com.bandyer.android_sdk.chat.notification.ChatEventBroadcastReceiver {

    private static final String TAG = "CHAT EVENT";

    @Override
    public void onChatStarted() {
        Log.d(TAG, "Chat started.");
    }

    @Override
    public void onChatEnded() {
        Log.d(TAG, "Chat ended.");
    }

    @Override
    public void onChatEndedWithError(@NonNull ChatException chatException) {
        Log.d(TAG, "Chat ended with error: " + chatException.getMessage());
    }
}
Clone this wiki locally