# 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.

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 v4.6.0

# API

# fetchUnreadMessageCount

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

void unreadMessageNumCallBack(int msgCount) {
    // write your code here
}
[AIHelpSupportSDK fetchUnreadMessageCount:unreadMessageNumCallBack];

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

  1. This method has a 5-minute 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 polling feature is not enabled.
  • When the user has no ongoing complaints.
  • When the user is on an AIHelp page.
  • When the user opens the customer service conversation window.

# By Polling

WARNING

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

# API

# startUnreadMessageCountPolling

Call this method to start a polling job for the count of unread messages.

void unreadMessageNumCallBack(int msgCount) {
    // write your code here
}
[AIHelpSupportSDK startUnreadMessageCountPolling:unreadMessageNumCallBack];

The count of unread messages is pulled every 5 minutes within the function.

This method will return 0 in the following situations to notify the caller that the current user has no unread messages:

  • When the polling feature is not enabled.
  • When the user has no ongoing complaints.
  • When the user is on an AIHelp page.
  • When the user opens the customer service conversation window.

# Definition

# onUnreadMessageCountCallback

  • Type: (void(*)(int unreadCount))
  • Details: Required. Callback for unread message count.

# By Third-Party Push

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

1、register APNs:

#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (@available(iOS 10.0, *)) {
        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                // The user has allowed push permissions
            }
        }];
    } else {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
}

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

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
   if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
       if (![deviceToken isKindOfClass:[NSData class]]) {
           return;
       }
       const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
       NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                             ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                             ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                             ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
       token = strToken;
   } else {
       token = [NSString stringWithFormat:@"%@", deviceToken];
       token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
       token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
       token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
   } 
   AIHelpUserConfigBuilder *userConfigBuider = [[AIHelpUserConfigBuilder alloc] init];
   userConfigBuider.userId = @"userId";
   [AIHelpSupportSDK updateUserInfo:userConfigBuider.build];
   [AIHelpSupportSDK setPushToken:token pushPlatform:AIHelpTokenPlatformAPNS];
}

3、alert your users when you receive a new message from AIHelp:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSDictionary *data = userInfo[@"data"];
    if (data && [data[@"elva"] isEqualToString:@"yes"]) {
        // 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/29/2023, 8:27:54 AM