一、优点
满足Web、App、微信公众号等全渠道的在线客户服务,支持客户信息展示,丰富沟通方式,超过100
项数据报表等功能 。一句话就是功能强大,聚合多个平台。
二、接入步骤:
1、在网易七鱼(http://qiyukf.com)网站注册一个账号,试用期7天,7天之后自动转为免费账号,可以同时支持两个客服在线,每个客服最大接待量只有一个,免费版适用于并发客服需求不大的情况。
2、进入后台添加一个APP
这个secret key没啥用。
3、按照文档一步步集成 http://qiyukf.com/newdoc/html/Android_SDK_Guide.html, 挑几个重点说,详细步骤看文档
4、application中初始化,appKey就是后台设置中的App Key。
Unicorn.init(this, appKey, options(), new FrescoImageLoader(this)); private YSFOptions options() { YSFOptions options = new YSFOptions(); options.statusBarNotificationConfig = new StatusBarNotificationConfig(); options.statusBarNotificationConfig.notificationEntrance = ActivityCustomerService.class; options.statusBarNotificationConfig.notificationSmallIconId = R.drawable.lauch_icon; options.onBotEventListener = new OnBotEventListener() { @Override public boolean onUrlClick(Context context, String url) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); return true; } }; return options; }
5、在进入聊天之前设置UI相关的配置,比如客服头像、标题栏、字体样式等,注意这里还有个 StatusBarNotificationConfig ,设置推送相关,这里设置之后,会覆盖application中的推送设置,所以如果这里没有设置StatusBarNotificationConfig或者没有设置正确,会导致收不到推送。
YSFOptions ysfOptions = new YSFOptions(); ysfOptions.statusBarNotificationConfig = new StatusBarNotificationConfig(); ysfOptions.statusBarNotificationConfig.notificationEntrance = ActivityCustomerService.class; ysfOptions.uiCustomization = new UICustomization(); // ysfOptions.uiCustomization.titleBarStyle = 0; // ysfOptions.uiCustomization.titleBackgroundColor = context.getResources().getColor(R.color // .header_bg); // ysfOptions.uiCustomization.topTipBarTextColor = context.getResources().getColor(R.color // .white); // ysfOptions.uiCustomization.titleCenter = true; ysfOptions.uiCustomization.leftAvatar = "android.resource://" + context.getPackageName() + "/" + R.drawable.header_kf; ysfOptions.uiCustomization.rightAvatar = "android.resource://" + context.getPackageName() + "/" + R.drawable.header_yh; // ysfOptions.uiCustomization.msgItemBackgroundLeft = R.drawable.kf_left; // ysfOptions.uiCustomization.textMsgColorLeft = context.getResources().getColor(R.color // .white); // ysfOptions.uiCustomization.msgItemBackgroundRight = R.drawable.kf_right; // ysfOptions.uiCustomization.textMsgColorRight = context.getResources().getColor(R.color // .text_black); Unicorn.updateOptions(ysfOptions);
6、监听未读消息数,当有新的消息时,在客服入口处提示
UnreadCountChangeListener listener = new UnreadCountChangeListener() { // 声明一个成员变量 @Override public void onUnreadCountChange(int count) { // 在此更新界面, count 为当前未读数, // 也可以用 Unicorn.getUnreadCount() 获取总的未读数 ToastView.showShort("未读:" + count); } }; Unicorn.addUnreadCountChangeListener(listener, true);7、设置用户信息,这里userBean是我自定义的用户对象。
YSFUserInfo userInfoA = new YSFUserInfo(); userInfoA.userId = beanUser.userId; userInfoA.data = userInfoData(beanUser.userName, beanUser.userCode, "", "", "", "") .toString(); Unicorn.setUserInfo(userInfoA, callback);
8、退出登录时要清空用户信息,不然设置新的用户信息时会无效。
Unicorn.setUserInfo(null, callback);
方法和设置用户信息的方法一样,只是把参数传空即可。注意,调用退出登录后,马上调用设置用户信息的方法,会设置失败,所以我写了一个延迟设置用户信息的方法,确保正确设置用户信息。完整在文章最后。
三、问题处理
1、我用自己的activity来装载七鱼的聊天fragment,发现当软件弹出时,输入框不会上移,导致看不见输入的内容。
找了很久发现是和我们设置沉浸式状态栏的代码有冲突
activity.getWindow().getDecorView() .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
这行代码加上就不行了,后来换了中处理沉浸式状态栏的方法。
2、收不到推送。
解决方法见上面接入步骤中的第5条。
四、完整代码
/** * 客服管理类 * * Created by huangyu on 2018/5/28. */ public class ManagerKefu { /** * 初始化UI * @param context */ public static void initUi(Context context) { YSFOptions ysfOptions = new YSFOptions(); ysfOptions.statusBarNotificationConfig = new StatusBarNotificationConfig(); ysfOptions.statusBarNotificationConfig.notificationEntrance = ActivityCustomerService.class; ysfOptions.uiCustomization = new UICustomization(); // ysfOptions.uiCustomization.titleBarStyle = 0; // ysfOptions.uiCustomization.titleBackgroundColor = context.getResources().getColor(R.color // .header_bg); // ysfOptions.uiCustomization.topTipBarTextColor = context.getResources().getColor(R.color // .white); // ysfOptions.uiCustomization.titleCenter = true; ysfOptions.uiCustomization.leftAvatar = "android.resource://" + context.getPackageName() + "/" + R.drawable.header_kf; ysfOptions.uiCustomization.rightAvatar = "android.resource://" + context.getPackageName() + "/" + R.drawable.header_yh; // ysfOptions.uiCustomization.msgItemBackgroundLeft = R.drawable.kf_left; // ysfOptions.uiCustomization.textMsgColorLeft = context.getResources().getColor(R.color // .white); // ysfOptions.uiCustomization.msgItemBackgroundRight = R.drawable.kf_right; // ysfOptions.uiCustomization.textMsgColorRight = context.getResources().getColor(R.color // .text_black); Unicorn.updateOptions(ysfOptions); } /** * 切换账号时清空信息 * * @param callback */ public static void logout(RequestCallback callback) { Unicorn.setUserInfo(null, callback); } /** * 延迟设置用户信息 * @param context * @param beanUser * @param callback */ public static void setUserInfoDelay(final Context context, final BeanUser beanUser, final RequestCallback callback) { new Handler().postDelayed(new Runnable() { @Override public void run() { setUserInfo(context, beanUser, callback); } }, 500); } /** * 设置用户信息 * @param context * @param beanUser * @param callback */ public static void setUserInfo(Context context, BeanUser beanUser, RequestCallback callback) { if (beanUser == null) { beanUser = new BeanUser(); beanUser.userId = DevicesUtil.getDeviceUniqueId(context); beanUser.userName = "游客"; } YSFUserInfo userInfoA = new YSFUserInfo(); userInfoA.userId = beanUser.userId; userInfoA.data = userInfoData(beanUser.userName, beanUser.userCode, "", "", "", "") .toString(); Unicorn.setUserInfo(userInfoA, callback); } private static JSONArray userInfoData(String name, String mobile, String email, String auth, String card, String order) { JSONArray array = new JSONArray(); array.put(userInfoDataItem("real_name", name, false, -1, null, null)); // name array.put(userInfoDataItem("mobile_phone", mobile, false, -1, null, null)); // mobile array.put(userInfoDataItem("email", email, false, -1, null, null)); // email array.put(userInfoDataItem("real_name_auth", auth, false, 0, "实名认证", null)); array.put(userInfoDataItem("bound_bank_card", card, false, 1, "绑定银行卡", null)); array.put(userInfoDataItem("recent_order", order, false, 2, "最近订单", null)); return array; } private static JSONObject userInfoDataItem(String key, Object value, boolean hidden, int index, String label, String href) { JSONObject item = new JSONObject(); try { item.put("key", key); item.put("value", value); if (hidden) { item.put("hidden", true); } if (index >= 0) { item.put("index", index); } if (!TextUtils.isEmpty(label)) { item.put("label", label); } if (!TextUtils.isEmpty(href)) { item.put("href", href); } } catch (JSONException e) { e.printStackTrace(); } return item; } /** * 只能清除文件缓存,无法清除聊天记录 */ public static void clearCache() { Unicorn.clearCache(); } }