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.     

To observe a chat you may add the following observer.

ChatObserver chatObserver = new ChatObserver() { };

ChatModule chatModule = BandyerSDKClient.getInstance().getChatModule();
if (chatModule == null) return;

chatModule.addChatObserver(chatObserver);
// or chatModule.addChatObserver(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 chat event broadcast receiver -->

<application>

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

        <receiver
            android:name=".MyChatEventBroadcastReceiver"
            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 MyChatEventBroadcastReceiver extends ChatEventBroadcastReceiver {

    private static final String TAG = "CHAT EVENT";

}
Clone this wiki locally