# Unread Message

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 proactively fetching or by third-party push.

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.

# By Fetching

# API

# fetchUnreadMessageCount / fetchUnreadTaskCount

Calling this method allows you to actively retrieve the unread message count for the current user.

AIHelpSupport.fetchUnreadMessageCount();
AIHelpSupport.fetchUnreadTaskCount();

You can call these APIs at any time to query the unread message count for the current user, but please note:

  1. This method has a frequency limit:

Repeated calls within the limit period will return the result of the previous query. During development, you can check the logs to see the time until the next valid call.

  1. This method will return 0 in the following situations to notify the caller that the current user has no unread messages:
  • When the user has no ongoing complaints or tasks.
  • After the user has opened the customer service conversation window, or checked all unread tasks.

# Event

Subscribe to the MESSAGE_ARRIVAL and UNREAD_TASK_COUNT events to receive notifications about unread message changes for the current user.

AsyncEventListener eventListener = (jsonEventData, ack) -> {
    // `jsonData`: { "eventType": 5, "msgCount": 1 }
    // `jsonData`: { "eventType": 8, "taskCount": 1 }
};
AIHelpSupport.registerAsyncEventListener(EventType.MESSAGE_ARRIVAL, eventListener);
AIHelpSupport.registerAsyncEventListener(EventType.UNREAD_TASK_COUNT, eventListener);

# 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: 7/25/2024, 4:33:52 AM