openimsdk / open-im-sdk-android

IM Android
https://openim.io
MIT License
225 stars 161 forks source link

After sending a message on Android, the listening interface doesn't detect any incoming messages #23

Closed Alicegril closed 2 years ago

Alicegril commented 2 years ago

package io.openim.example.test;

import android.os.Bundle; import android.util.Log; import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import io.openim.android.sdk.OpenIMClient; import io.openim.android.sdk.listener.InitSDKListener; import io.openim.android.sdk.listener.OnAdvanceMsgListener; import io.openim.android.sdk.listener.OnBase; import io.openim.android.sdk.listener.OnConversationListener; import io.openim.android.sdk.listener.OnFriendshipListener; import io.openim.android.sdk.listener.OnGroupListener; import io.openim.android.sdk.listener.OnMsgSendCallback; import io.openim.android.sdk.models.ConversationInfo; import io.openim.android.sdk.models.GroupInfo; import io.openim.android.sdk.models.GroupMembersInfo; import io.openim.android.sdk.models.HaveReadInfo; import io.openim.android.sdk.models.Message; import io.openim.android.sdk.models.UserInfo; import io.openim.android.sdk.utils.JsonUtil; import io.openim.example.R; import io.openim.example.httplibrary.utils.ChineseUtils;

public class MainTestActivity extends AppCompatActivity implements InitSDKListener, OnAdvanceMsgListener, OnConversationListener, OnFriendshipListener, OnGroupListener {

/// api地址
final static String IP_API = "http://" + BaseConent.IP_URL + ":10000";
/// websocket地址
final static String IP_WS = "ws://" + BaseConent.IP_URL + ":30000";

OpenIMClient client;

// private String uuid = "18092397182"; // private String uuidRecss = "15229015072"; // private String Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiIxODA5MjM5NzE4MiIsIlBsYXRmb3JtIjoiQW5kcm9pZCIsImV4cCI6MTYzNzc0MjU0MywibmJmIjoxNjM3MTM3NzQzLCJpYXQiOjE2MzcxMzc3NDN9.TWUc8l1HdPdq4ZPeVF_lFBjI_J5MY5NQKSWbMqFeiMU";

private String TAG = "--------------";
private String uuid = "15229015072";
private String uuidRecss = "18092397182";
private String Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiIxNTIyOTAxNTA3MiIsIlBsYXRmb3JtIjoiQW5kcm9pZCIsImV4cCI6MTYzNzc0MjYzNSwibmJmIjoxNjM3MTM3ODM1LCJpYXQiOjE2MzcxMzc4MzV9.pCQvzlZR6y06XxCPItcJ1WvPywK6QZ-BTMY-wWIF3cA";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_main);
    String path = getExternalCacheDir().getAbsolutePath();
    client = OpenIMClient.getInstance();
    client.initSDK(IP_API, IP_WS, path, this);

    client.groupManager.setOnGroupListener(this);
    client.conversationManager.setOnConversationListener(this);
    client.friendshipManager.setOnFriendshipListener(this);
    client.messageManager.addAdvancedMsgListener(this);

}

@Override
public void onConnectFailed(long code, String error) {
    Log.e("----------", "onConnectFailed" + "--code--" + code + "--error--" + error);

}

@Override
public void onConnectSuccess() {
    Log.e("----------", "onConnectSuccess");
}

@Override
public void onConnecting() {
    Log.e("----------", "onConnecting");

}

@Override
public void onKickedOffline() {
    Log.e("----------", "onKickedOffline");

}

@Override
public void onSelfInfoUpdated(UserInfo info) {
    Log.e("----------", "onSelfInfoUpdated--" + JsonUtil.toString(info));

}

@Override
public void onUserTokenExpired() {
    Log.e("----------", "onUserTokenExpired");
}

@Override
public void onRecvNewMessage(Message msg) {
    Log.e("----------", "onRecvNewMessage--" + JsonUtil.toString(msg));

}

@Override
public void onRecvC2CReadReceipt(List<HaveReadInfo> list) {
    Log.e("----------", "onRecvC2CReadReceipt--" + list.size() + "-----" + JsonUtil.toString(list));

}

@Override
public void onRecvMessageRevoked(String msgId) {
    Log.e("----------", "onRecvMessageRevoked" + "--msgId--" + msgId);
}

@Override
public void onConversationChanged(List<ConversationInfo> list) {
    Log.e("----------", "onConversationChanged--" + list.size() + "-----" + JsonUtil.toString(list));

}

@Override
public void onNewConversation(List<ConversationInfo> list) {
    Log.e("----------", "onNewConversation--" + list.size() + "-----" + JsonUtil.toString(list));
}

@Override
public void onSyncServerFailed() {
    Log.e("----------", "onSyncServerFailed--");
}

@Override
public void onSyncServerFinish() {
    Log.e("----------", "onSyncServerFinish--");
}

@Override
public void onSyncServerStart() {
    Log.e("----------", "onSyncServerStart--");
}

@Override
public void onTotalUnreadMessageCountChanged(int i) {
    Log.e("----------", "onTotalUnreadMessageCountChanged--i" + i);
}

@Override
public void onBlackListAdd(UserInfo u) {
    Log.e("----------", "onBlackListAdd--" + JsonUtil.toString(u));
}

@Override
public void onBlackListDeleted(UserInfo u) {
    Log.e("----------", "onBlackListDeleted--" + JsonUtil.toString(u));
}

@Override
public void onFriendApplicationListAccept(UserInfo u) {
    Log.e("----------", "onFriendApplicationListAccept--" + JsonUtil.toString(u));
}

@Override
public void onFriendApplicationListAdded(UserInfo u) {
    Log.e("----------", "onFriendApplicationListAdded--" + JsonUtil.toString(u));
}

@Override
public void onFriendApplicationListDeleted(UserInfo u) {
    Log.e("----------", "onFriendApplicationListDeleted--" + JsonUtil.toString(u));
}

@Override
public void onFriendApplicationListReject(UserInfo u) {
    Log.e("----------", "onFriendApplicationListReject--" + JsonUtil.toString(u));
}

@Override
public void onFriendInfoChanged(UserInfo u) {
    Log.e("----------", "onFriendInfoChanged--" + JsonUtil.toString(u));
}

@Override
public void onFriendListAdded(UserInfo u) {
    Log.e("----------", "onFriendListAdded--" + JsonUtil.toString(u));
}

@Override
public void onFriendListDeleted(UserInfo u) {
    Log.e("----------", "onFriendListDeleted--" + JsonUtil.toString(u));
}

@Override
public void onMemberEnter(String groupId, List<GroupMembersInfo> list) {
    Log.e("----------", "onFriendListDeleted--" + "groupId--" + groupId + "---" + list.size() + "-----" + JsonUtil.toString(list));
}

@Override
public void onMemberLeave(String groupId, GroupMembersInfo info) {
    Log.e("----------", "onMemberLeave--" + "groupId--" + groupId + "---" + JsonUtil.toString(info));
}

@Override
public void onMemberInvited(String groupId, GroupMembersInfo opUser, List<GroupMembersInfo> list) {
    Log.e("----------", "onMemberInvited--" + "groupId--" + groupId + "---" + JsonUtil.toString(opUser) + "---" + list.size() + "-----" + JsonUtil.toString(list));
}

@Override
public void onMemberKicked(String groupId, GroupMembersInfo opUser, List<GroupMembersInfo> list) {
    Log.e("----------", "onMemberKicked--" + "groupId--" + groupId + "---" + JsonUtil.toString(opUser) + "---" + list.size() + "-----" + JsonUtil.toString(list));
}

@Override
public void onGroupCreated(String groupId) {
    Log.e("----------", "onGroupCreated--" + "groupId--" + groupId);

}

@Override
public void onGroupInfoChanged(String groupId, GroupInfo info) {
    Log.e("----------", "onGroupInfoChanged--" + "groupId--" + groupId + "---" + JsonUtil.toString(info));

}

@Override
public void onReceiveJoinApplication(String groupId, GroupMembersInfo info, String opReason) {

    Log.e("----------", "onReceiveJoinApplication" + "--groupId--" + groupId + "--opReason--" + opReason);
}

@Override
public void onApplicationProcessed(String groupId, GroupMembersInfo opUser, int agreeOrReject, String opReason) {
    Log.e("----------", "onApplicationProcessed" + "--groupId--" + groupId + "--agreeOrReject--" + agreeOrReject + "--opReason--" + opReason);
}

public void onGetConversation(View view) {
    String chinese = ChineseUtils.randomWord(3);
    Log.e("----------", "chinese--" + chinese);

    Message msg = client.messageManager.createTextMessage(chinese);
    client.messageManager.sendMessage(new OnMsgSendCallback() {
        @Override
        public void onError(long code, String error) {
            Log.e("----------", "sendMessage--" + code + "--" + error);
        }

        @Override
        public void onProgress(long progress) {
            Log.e("----------", "sendMessage--progress" + progress);
        }

        @Override
        public void onSuccess(String s) {
            Log.e("----------", "sendMessage--" + s);
        }
    }, msg, uuid, null, false);
}

public void onLogin(View view) {
    client.login(new OnBase<String>() {
        @Override
        public void onError(long code, String error) {
            Log.e("----------", "--" + code + "--" + error);

        }

        @Override
        public void onSuccess(String data) {
            Log.e("----------", "--" + data);

        }
    }, uuid, Token);
}

public void addFriend(View view) {

    Message message = client.messageManager.createTextMessage("看见哈萨克");

    client.messageManager.sendMessage(new OnMsgSendCallback() {
        @Override
        public void onError(long l, String s) {
            Log.e(TAG, "onError--" + l + "--" + s);
        }

        @Override
        public void onProgress(long l) {
            Log.e(TAG, "onProgress--" + l);
        }

        @Override
        public void onSuccess(String s) {

            Log.e(TAG, "onSuccess--" + s);
        }
    }, message, uuid, null, false);
}

public void acceptFriendApplication(View view) {
    client.friendshipManager.acceptFriendApplication(new OnBase<String>() {
        @Override
        public void onError(long code, String error) {
            Log.e("----------", "--" + code + "--" + error);

        }

        @Override
        public void onSuccess(String data) {
            Log.e("----------", "--" + data);

        }
    }, uuidRecss);
}

public void onGetMessageHistory(View view) {
    client.messageManager.getHistoryMessageList(new OnBase<List<Message>>() {
        @Override
        public void onError(long code, String error) {
            Log.e("----------", "--" + code + "--" + error);
        }

        @Override
        public void onSuccess(List<Message> data) {
            Log.e("----------", "--消息数:" + "--" + data.size() + "--" + JsonUtil.toString(data));

        }
    }, uuidRecss, "", null, 200);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    client.logout(new OnBase<String>() {
        @Override
        public void onError(long code, String error) {
            Log.e("----------", "logout--" + code + "--" + error);
        }

        @Override
        public void onSuccess(String data) {
            Log.e("----------", "logout--" + data);
        }
    });

}

}

hrxiang commented 2 years ago

收消息通过全局onRecvNewMessage回调,发消息通过sendMessage的callbak

监听器都必须在调用login方法之前设置。

Topone commented 2 years ago

IP_WS 端口改成 17778