# Unread Message Count

When the customer service replies user's message, user can receive a notification of unread messages, even if the AIHelp page is not currently displayed.

You can implement this function either by every-5-minutes' polling or by third-party push.

TIP

This feature is disabled by default, please contact us before your integration.

# By Polling

WARNING

By default, AIHelp uses a randomly generated deviceId as userId to poll for the count of unread messages.

So, until you sync the correct userId to AIHelp, the unread message count may be inaccurate.

# API

# startUnreadMessageCountPolling()

Call this method to start a polling job for the count of unread messages. Please ensure that you are calling this method after the initialization is finished.






 


 









AIHelpSupport.setOnAIHelpInitializedCallback(new OnAIHelpInitializedCallback() {
    @Override
    public void onAIHelpInitialized() {

        // Sync userId info to AIHelp via updateUserInfo API
        AIHelpSupport.updateUserInfo(new UserConfig.Builder().setUserId("uid").build());
        
        // Start polling
        AIHelpSupport.startUnreadMessageCountPolling(new OnMessageCountArrivedCallback() {
            @Override
            public void onMessageCountArrived(int msgCount) {
                // write your code here
            }
        });
        
    }
});

The count of unread messages is pulled every 5 minutes within the function, the result will be returned to the caller in the following conditions:

  1. When user with ongoing tickets receives a new message, accumulated number of unread messages will be returned;
  2. When users open AIHelp session, a returning with number 0 indicates they have read all messages.

# Definition

# onUnreadMessageCountCallback

  • Type: OnMessageCountArrivedCallback
  • Detail: Required. Callback for unread message count.

# By Third-Party Push

Now we take firebase as an example to illustrate how the in-app notification works.

1、follow up Google Documentation (opens new window) to integrate Firebase in your app;

2、call the following method to inform AIHelp of the player's push token and push platform

AIHelpSupport.updateUserInfo(new UserConfig.Builder().setUserId("uid").build());
AIHelpSupport.setPushTokenAndPlatform("PUSH_TOKEN", PushPlatform.FIREBASE);

3、override onMessageReceived method in your custom FirebaseMessagingService.java, and alert your users when you receive a new message while your app is foreground:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    // Check if current application is foreground and visible to users
    if (isAppForeground()) {
        if (remoteMessage.getData() != null) {
            if ("yes".equals(remoteMessage.getData().get("elva"))) {
                // alert your users that he/she has received a new message
            }
        }
    }
}

4、AIHelp push data format is as follows:

{
    "to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
        "body":"This is your new message content",
        "title":"customer service sends you a message.",
        "sound":"Enabled",
        "priority":"high",
        "uid":"..."
    },
    "data":{
        "body":"This is your new message content",
        "title":"customer service sends you a message.",
        "uid":"...",
        "elva":"yes",
        "entry_tag":"..."
    }
}
Last Updated: 12/19/2023, 3:49:07 AM