Android应用客服系统解决方案---网易七鱼客服


应用客服系统

现在好多应用都有客服系统需求,常见问题可以通过智能客服解决,疑难问题可以联系人工客服实时解答。比较常见的购物应用的客服功能。

七鱼客服

七鱼 Android SDK Github
网易七鱼 Android SDK 开发指南

七鱼SDK接入

1.添加依赖
compile 'com.qiyukf.unicorn:unicorn:4.8.0'
2.初始化七鱼客服

在Application 中初始化,初始化时最好将YSFOptions 提取出来,便于后面根据需要修改七鱼的配置。

public class MyApplication extends Application {

    /**
     * 网易七鱼客服
     * 这里将YSFOptions提取出来是为了便于后面对七鱼配置的修改
     */
    public static YSFOptions ysfOptions;

    @Override
    public void onCreate() {
        super.onCreate();
        //网易七鱼客服
        Unicorn.init(this, "appKey", options(), new GlideImageLoader(this));
    }

    /**
     * 网易七鱼客服
     *
     * @return
     */
    private YSFOptions options() {
        YSFOptions options = new YSFOptions();
        /**
         * 客服消息通知
         */
        options.statusBarNotificationConfig = new StatusBarNotificationConfig();
        options.statusBarNotificationConfig.notificationSmallIconId = R.mipmap.ic_launcher;
        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;
            }
        };

        ysfOptions = options;
        return options;
    }

}

我这里图片加载用的是Glide,所以配置的是Glide,使用其他图片加载框架的参考官方API中图片加载
这里是基于Glide3.7的写法。

public class GlideImageLoader implements UnicornImageLoader {
    private Context context;

    public GlideImageLoader(Context context) {
        this.context = context.getApplicationContext();
    }

    @Nullable
    @Override
    public Bitmap loadImageSync(String uri, int width, int height) {
        return null;
    }

    @Override
    public void loadImage(String uri, int width, int height, final ImageLoaderListener listener) {
        if (width <= 0 || height <= 0) {
            width = height = Integer.MIN_VALUE;
        }

        Glide.with(context).load(uri).asBitmap().into(new SimpleTarget<Bitmap>(width, height) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                if (listener != null) {
                    listener.onLoadComplete(resource);
                }
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                if (listener != null) {
                    listener.onLoadFailed(e);
                }
            }
        });
    }
}

对于Glide4.0以上的写法,有些细微变化

RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.placeholder)
                .centerCrop()
                .error(R.drawable.error);
 Glide.with(context).asBitmap().load(uri).apply(options)
                .into(new SimpleTarget<Bitmap>(width, height) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                if (listener != null) {
                    listener.onLoadComplete(resource);
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
                Throwable t = new Throwable("加载异常");
                listener.onLoadFailed(t);
            }
        });
3.设置当前访问用户的信息

为了便于管理用户,可设置访问用户的信息

	YSFUserInfo userInfo = new YSFUserInfo();
    // App 的用户 ID
    userInfo.userId = "用户ID";
    // CRM 扩展字段
    userInfo.data = userInfoData("用户名字", "用户手机号", "用户头像");
    Unicorn.setUserInfo(userInfo);
4.设置未读消息监听

在进入客服对话页面之前,我们可以先设置一个入口,入口处可展示未读消息数目和最近一条消息内容。

Unicorn.addUnreadCountChangeListener(new UnreadCountChangeListener() {
            @Override
            public void onUnreadCountChange(int count) {
                // count 为当前未读数,
                // custNoReadCount = count;
                /**
                 * 获取和客服的最后一条聊天消息内容。
                 * 可用于未读消息变化时,展示最后一条未读消息,或者展示客服的最后一条消息。
                 * @return 最后一条消息
                 */
                List<Session> sessionList = UnicornManager.getSessionList();
                if (sessionList != null && sessionList.size() > 0) {
                    Session message = sessionList.get(0);
                    if (message != null && !TextUtils.isEmpty(message.getContent())) {
                        tvMsg.setText(message.getContent());
                    } else {
                        tvMsg.setText("暂无消息");
                    }
                }
            }
        },true);
    /**
     * 构造用户信息
     *
     * @param name   名字
     * @param mobile 手机
     * @param avatar 头像
     * @return
     */
    private static String userInfoData(String name, String mobile, String avatar) {
        List<YSFUser> mListUser = new ArrayList<>();
        YSFUser rName = new YSFUser("real_name", name);
        YSFUser rMoblie = new YSFUser("mobile_phone", mobile);
        YSFUser rAvatar = new YSFUser("avatar", avatar);
        mListUser.add(rName);
        mListUser.add(rMoblie);
        mListUser.add(rAvatar);
        return new Gson().toJson(mListUser);
    }
/**
 * 七鱼用户信息
 */
public class YSFUser {
    String key;
    String value;

    public YSFUser(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

注:官方API中获取最后一条方法为Unicorn.queryLastMessage();,但我在使用时发现了一个问题,这个方法只能获取用户和人工客服的最后一条消息,智能客服的消息获取不到;所以我使用的是获取会话列表的方法,然后取出列表中的第一条消息,即最新一条消息。

5.进入客服对话页面

客服入口设置完成之后,先对客服的一些配置进行修改

MyApplication.ysfOptions.uiCustomization = uiCustomization();

    /**
     * 设置对话UI(七鱼客服)
     *
     * @return
     */
    private static UICustomization uiCustomization() {
        // 以下示例的图片均无版权,请勿使用
        UICustomization customization = new UICustomization();
        //客服窗口UI自定义
        customization = new UICustomization();
        //标题栏背景颜色
        customization.titleBackgroundResId = R.color.colorPrimary;
        //标题栏风格,影响标题和标题栏上按钮的颜色(0浅色系,1深色系)
        customization.titleBarStyle = 1;
        //键盘控制
        customization.hideKeyboardOnEnterConsult = true;
        //输入框内字体颜色(一定要设置这个属性,有的手机上不设置此属性会出现输入的字体色是透明的)
        customization.inputTextColor = MyApplication.getContext().getResources().getColor(R.color.base_text);
        //访问者头像
        customization.rightAvatar = "";
        //客服头像
        // customization.leftAvatar = url;

        return customization;
    }

注:设置对话页面UI时,一定要设置inputTextColor这个属性是对话输入框中的字体颜色;我在使用过程中遇到过一个问题,有些手机的输入框内的字体是白色的,导致我一直以为无法输入,后来才发现字体是白色的。

6.其他方法
  • 当用户退出登录时,也需要将七鱼注销
	Unicorn.logout();
  • 有些应用会涉及到清除应用缓存,七鱼也提供了清除缓存的方法
	Unicorn.clearCache();

结束语

文章只讲述了一些常用方法,更多详细的功能使用可参考官方API,为了便于使用,我将这些常用方法整理成一个管理工具类

public class UnicornManager {
    /**
     * 进入智能客服页面
     *
     * @param context
     */
    public static void inToUnicorn(Context context) {
        /**
         * 设置访客来源,标识访客是从哪个页面发起咨询的,用于客服了解用户是从什么页面进入。
         * 三个参数分别为:来源页面的url,来源页面标题,来源页面额外信息(保留字段,暂时无用)。
         * 设置来源后,在客服会话界面的"用户资料"栏的页面项,可以看到这里设置的值。
         */
        ConsultSource source = new ConsultSource(null, null, null);
//        source.prompt = "连接客服成功的提示语";
//        source.VIPStaffAvatarUrl = "头像的 url";
//        source.vipStaffName = "客服的的名字";
//        source.vipStaffWelcomeMsg = "客服的欢迎语";
        /**
         * 请注意: 调用该接口前,应先检查Unicorn.isServiceAvailable(),
         * 如果返回为false,该接口不会有任何动作
         *
         * @param context 上下文
         * @param title   聊天窗口的标题
         * @param source  咨询的发起来源,包括发起咨询的url,title,描述信息等
         */
        String title = "七鱼客服";
        Unicorn.openServiceActivity(context, title, source);
    }

    /**
     * 设置设置对话UI
     */
    public static void setUiCustomization() {
        MyApplication.ysfOptions.uiCustomization = uiCustomization();
    }

    /**
     * 设置对话UI(七鱼客服)
     *
     * @return
     */
    private static UICustomization uiCustomization() {
        // 以下示例的图片均无版权,请勿使用
        UICustomization customization = new UICustomization();
        //客服窗口UI自定义
        customization = new UICustomization();
        //标题栏背景颜色
        customization.titleBackgroundResId = R.color.colorPrimary;
        //标题栏风格,影响标题和标题栏上按钮的颜色(0浅色系,1深色系)
        customization.titleBarStyle = 1;
        //键盘控制
        customization.hideKeyboardOnEnterConsult = true;
        //输入框内字体颜色(一定要设置这个属性,有的手机上不设置此属性会出现输入的字体色是透明的)
        customization.inputTextColor = MyApplication.getContext().getResources().getColor(R.color.base_text);
        //访问者头像
        customization.rightAvatar = "";
        //客服头像
        // customization.leftAvatar = url;

        return customization;
    }

    /**
     * 设置用户信息(七鱼客服)
     */
    public static void setUnicornUserInfo() {
        YSFUserInfo userInfo = new YSFUserInfo();
        // App 的用户 ID
        userInfo.userId = "用户ID";
        // CRM 扩展字段
        userInfo.data = userInfoData("用户名字", "用户手机号", "用户头像");
        Unicorn.setUserInfo(userInfo);
    }

    /**
     * 构造用户信息
     *
     * @param name   名字
     * @param mobile 手机
     * @param avatar 头像
     * @return
     */
    private static String userInfoData(String name, String mobile, String avatar) {
        List<YSFUser> mListUser = new ArrayList<>();
        YSFUser rName = new YSFUser("real_name", name);
        YSFUser rMoblie = new YSFUser("mobile_phone", mobile);
        YSFUser rAvatar = new YSFUser("avatar", avatar);
        mListUser.add(rName);
        mListUser.add(rMoblie);
        mListUser.add(rAvatar);
        return new Gson().toJson(mListUser);
    }

    /**
     * 客服未读消息数变化监听
     *
     * @param listener
     */
    public static void addUnreadCountChangeListener(final UnreadCountChangeListener listener) {
        Unicorn.addUnreadCountChangeListener(listener, true);
    }

    /**
     * 获取最后一条消息
     */
    public static UnicornMessage queryLastMessage() {
        UnicornMessage message = Unicorn.queryLastMessage();
        return message;
    }

    /**
     * 获取客服与商家消息列表
     */
    public static List<Session> getSessionList() {
        List<Session> sessionList = POPManager.getSessionList();
        return sessionList;
    }

    /**
     * 网易七鱼客服
     * 清除文件缓存,将删除SDK接收过的所有文件。<br>
     * 建议在工作线程中执行该操作。
     * 该操作放到设置中 清除缓存操作下
     */
    public static void clearCache() {
        Unicorn.clearCache();
    }

    /**
     * 注销网易七鱼客服
     */
    public static void logout() {
        Unicorn.logout();
    }
}

demo地址

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值