知识扩展Android学习Android知识

Android-IM使用极光即时通讯发送、接收好友请求

2017-07-27  本文已影响552人  八怪不姓丑

首先需要知道整个流程,包括发送方,和接收方

发送请求比较简单
如果集成了JMessage可以直接使用下面的类

sendInvitationRequest方法包含三个参数,分别是目标id,备注,和返回结果。

ContactManager.sendInvitationRequest(userName,content, new BasicCallback() {
                        @Override
                        public void gotResult(int i, String s) {
                            if (i==0){
                                showToast(AddFriendMsgActivity.this, "发送成功");
                            }else {
                                showToast(AddFriendMsgActivity.this, "发送失败:"+s);
                            }
                        }
                    });

请求相对来说比较复杂一些,需要我们手动去接收消息。

首先是需要注册消息事件,有两种地方。但是都要在OnCreate方法体内先注册事件接收者

JMessageClient.registerEventReceiver(this);

如果需要使用极光的Event还要注册:

EventBus.getDefault().register(this);

注册完后要重写线程方法
一种是主线程:

  public void onEventMainThread(ContactNotifyEvent event) {
        String message = event.getReason();

        Log.e("bean===", event.getFromUsername() + "," + event.getReason());
    }

另一种是子线程:

public void onEvent(EventEntity event){
 
}

然后我们来打印两个客户端进行传递消息的日志。

JPushIM.png
07-27 15:50:22.133 19990-19990/com.wapchief.jpushim E/bean===: 1004,你好!我是测试老四
07-27 16:02:58.727 21015-21015/com.wapchief.jpushim E/bean===: 1004,你好!我是测试老四1,invite_received
07-27 16:03:03.652 21015-21015/com.wapchief.jpushim E/bean===: 1004,你好!我是测试老四1,2,invite_received
07-27 16:03:29.118 21015-21015/com.wapchief.jpushim E/bean===: 1004,你好!我是测试老四1,2,invite_received

最后别忘了销毁

    @Override
    protected void onDestroy() {
        //销毁
        JMessageClient.unRegisterEventReceiver(this);
        super.onDestroy();

    }

PS
中间还遇到了一个Bug

has no public methods called onEvent
//没有公共方法叫做onEvent

检查是否注册EventBus
和重写onEvent


项目地址:https://github.com/wapchief/Android-IM

相关阅读推荐:


文档参考:
https://docs.jiguang.cn/jmessage/client/im_android_api_docs/
https://docs.jiguang.cn/jmessage/client/im_sdk_android/#_45
https://stackoverflow.com/questions/24926859/subscriber-class-has-no-public-methods-called-on-event

上一篇下一篇

猜你喜欢

热点阅读