# 未读消息

当客服人员回复了用户的消息后,用户可以收到未读消息的提醒,即使当前并没有展示 AIHelp 的页面。

你可以通过主动拉取或者第三方推送实现这个功能。

注意

AIHelp 默认使用随机生成的 deviceId 作为 userId 进行未读消息数量的轮询。

所以,在你将正确的 userId 同步给 AIHelp 之前,未读消息数量可能会不准确。

# 主动拉取

# API

# fetchUnreadMessageCount / fetchUnreadTaskCount

调用此方法可以主动拉取当前用户的未读消息数量。

[AIHelpSupportSDK fetchUnreadMessageCount];
[AIHelpSupportSDK fetchUnreadTaskCount];

你可以在任意时间调用该 API 查询当前用户的未读信息,但请注意:

  1. 上述方法内部都有默认的频率限制:

在限制期内的重复调用,会返回上一次的查询结果;在开发阶段,可以通过日志查看距离下一次有效调用的时间。

  1. 该方法会在以下情况返回 0,以通知调用者当前用户没有未读消息:
  • 用户没有进行中客诉或进行中工单时;
  • 用户打开客服会话窗口或已读所有工单信息时;

# 事件

通过监听 AIHelpEventMessageArrivalAIHelpEventUnreadTaskCount 事件,你可以收到当前用户的未读消息情况:

void listener(const char *eventData, void (*acknowledge)(const char *ackData)) {
    // `eventData`: { "eventType": 5, "msgCount": 1 }
    // `eventData`: { "eventType": 8, "taskCount": 1 }
}

[AIHelpSupportSDK registerAsyncListener:listener eventType:AIHelpEventMessageArrival];
[AIHelpSupportSDK registerAsyncListener:listener eventType:AIHelpEventUnreadTaskCount];

# 推送方案

下面以 APNs 平台为例,说明如何利用第三方推送平台实现应用内未读消息的回调。

1、注册 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、在代码中调用推送相关的方法,将用户的推送标识以及推送平台告知 AIHelp:

- (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、在回调中解析 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 推送数据格式如下:

{
    "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":"..."
    }
}
上次更新: 7/25/2024, 4:33:52 AM