# 未读消息数回调
当客服人员回复了用户的消息后,用户可以收到未读消息的提醒,即使当前并没有展示 AIHelp 的页面。
你可以通过每五分钟一次的轮询作业或者第三方推送实现这个功能。
提示
此功能默认为关闭状态,如有需要,请联系 AIHelp 运营人员开启此功能。
# 轮询方案
# API
# startUnreadMessageCountPolling
调用此方法可以启动未读消息数量的轮询作业,请确保初始化成功之后再调用此方法。
void AIHelp_onInitializationCallback() {
// Sync userId info to AIHelp via updateUserInfo API
[AIHelpSupportSDK updateUserInfo:[[[AIHelpUserConfigBuilder alloc] init] build]];
// Start polling
[AIHelpSupportSDK startUnreadMessageCountPolling:unreadMessageNumCallBack];
}
方法内部每 5 分钟主动拉取一次当前用户的未读消息数量,并在以下两种情况时将结果返回给调用者:
1、有进行中客诉的用户收到新消息时,返回该用户累计未读的消息数量;
2、用户打开客服会话窗口时,返回数字 0 以标记用户已读当前消息。
# 参数释义
# onUnreadMessageCountCallback
- 类型:
(void(*)(int unreadCount))
- 详情:必传参数。 未读消息数量的回调。
# 推送方案
下面以 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":"..."
}
}