Android Kotlin Pusher Chatkit-错误-需要房间会员 [英] Android Kotlin Pusher Chatkit - error - Room membership required

查看:113
本文介绍了Android Kotlin Pusher Chatkit-错误-需要房间会员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将chatkit集成到我的Android应用中,以获取这部分入门中的部分代码教程和此 android-public-demo-app 项目github,我收到此错误:

I'm trying to integrate chatkit into my Android app grabbing portions of code from this getting started tutorial and this android-public-demo-app project on github and I am getting this error:

D/ChatRoomsActivity: on subscripetoRoomMultipart reason:: Room membership required.

根据该帖子底部显示的仪表板/控制台摘要,用户已经是该会议室的成员,这是一个错误.当前currentUser是:user id=username2-PCKid

The user is already a member of the room which is producing is an error according to the dashboard/console snippets which are shown at the bottom of this post. Currently the currentUser is: user id=username2-PCKid

currentUser.subscribeToRoomMultipart处的ChatRoomAcitivity.kt 中发生错误.我包括了ChatRoomListActivity和上下文适配器.

Error occurs in ChatRoomAcitivity.kt at currentUser.subscribeToRoomMultipart. I included the ChatRoomListActivity and adapters for context.

任何帮助都将受到赞赏.请让我知道是否需要更多上下文.

Any and all help is appreciated. Please let me know if more context is required.

这是我的 ChatRoomListActivity.kt

class ChatRoomsListActivity : AppCompatActivity() {
    val adapter = ChatRoomsListAdapter();

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat_room_list)
        initRecyclerView()
        initChatManager()

    }

    private fun initRecyclerView() {

        recycler_view.layoutManager = LinearLayoutManager(this@ChatRoomsListActivity)
        recycler_view.adapter = adapter
    }

    private fun initChatManager() {
        val chatManager = ChatManager(
                instanceLocator = "************",
                userId = "username2-PCKid",
                dependencies = AndroidChatkitDependencies(
                        tokenProvider = ChatkitTokenProvider(
                                endpoint = "******************",
                                userId = "username2-PCKid"
                        )
                )
        )

        chatManager.connect(listeners = ChatListeners(

        )
                , callback = { result ->
            when (result) {
                is Result.Success -> {

                    // We have connected!
                    Log.d(AppActivityTags.chatRoomsListActivityTAG, "chatManager connected!")
                    val currentUser = result.value
                    AppController.currentUser = currentUser
                    Log.d(AppActivityTags.chatRoomsListActivityTAG, "user: " + currentUser + " is logged in to chatkit")

                    val userJoinedRooms = ArrayList<Room>()

                    for (x in currentUser.rooms) {
                            adapter.addRoom(x)
                            recycler_view.smoothScrollToPosition(0)
                    }

                    adapter.notifyDataSetChanged()

                    Log.d(AppActivityTags.chatRoomsListActivityTAG, "joined rooms.size: " + userJoinedRooms.size.toString());

                    adapter.setInterface(object : ChatRoomsListAdapter.RoomClickedInterface {

                        override fun roomSelected(room: Room) {
                            Log.d(AppActivityTags.chatRoomsListActivityTAG, "Room clicked!")
                            if (room.memberUserIds.contains("username2-PCKid")) {
//                            if (room.memberUserIds.contains(currentUser.id)) { <-- OG code
                                // user already belongs to this room
                                roomJoined(room)
                                Log.d("roomSelected", "user already belongs to this room: " + roomJoined(room))
                            } else {
                                currentUser.joinRoom(
                                        roomId = room.id,
                                        callback = { result ->
                                            when (result) {
                                                is Result.Success -> {
                                                    // Joined the room!
                                                    roomJoined(result.value)
                                                }
                                                is Result.Failure -> {
                                                    Log.d(AppActivityTags.chatRoomsListActivityTAG, result.error.toString())
                                                }
                                            }
                                        }
                                )
                            }
                        }
                    })
                }

                is Result.Failure -> {
                    // Failure
                    Log.d(AppActivityTags.chatRoomsListActivityTAG, "ChatManager connection failed"
                            + result.error.toString())
                }
            }
        })

    }

    private fun roomJoined(room: Room) {
        val intent = Intent(this@ChatRoomsListActivity, ChatRoomActivity::class.java)
        Log.d(AppActivityTags.chatRoomsListActivityTAG, "function roomJoined activated")
        intent.putExtra("room_id", room.id)
        intent.putExtra("room_name", room.name)
        startActivity(intent)
    }
}

这是我的 ChatRoomListAdapter.kt

class ChatRoomsListAdapter: RecyclerView.Adapter<ChatRoomsListAdapter.ViewHolder>() {

    private var list = ArrayList<Room>()
    private var roomClickedInterface: RoomClickedInterface? = null

    fun addRoom(room:Room){

        list.add(room);
        Log.d(AppActivityTags.chatRoomsListAdapterTAG, "Room name: " + room.name)
        Log.d(AppActivityTags.chatRoomsListAdapterTAG, "Room id: " + room.id)
        Log.d(AppActivityTags.chatRoomsListAdapterTAG, "Room memberUserIds: " + room.memberUserIds)
        Log.d(AppActivityTags.chatRoomsListAdapterTAG, "Room isPrivate: " + room.isPrivate)
    }


    fun setInterface(roomClickedInterface:RoomClickedInterface){
        this.roomClickedInterface = roomClickedInterface
    }

    override fun getItemCount(): Int {
        return list.size
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(
                        android.R.layout.simple_list_item_1,
                        parent,
                        false
                )

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.roomName.text = list[position].name
        val context = holder.itemView.context


        holder.itemView.setOnClickListener {

            room = list[position]

            val intent = Intent(context, ChatRoomActivity::class.java)
            Log.d(AppActivityTags.chatRoomsListActivityTAG, "function roomJoined activated")
            intent.putExtra("room_id", room.id)
            intent.putExtra("room_name", room.name)
            context.startActivity(intent)
        }
    }


    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
        override fun onClick(p0: View?) {
            roomClickedInterface?.roomSelected(list[adapterPosition])
            Toast.makeText(itemView.context, "item was clicked", Toast.LENGTH_LONG).show()

            val mContext = itemView.context
            Log.d(AppActivityTags.chatRoomsListAdapterTAG, "Size of adapter: " + list.size.toString())
            Log.d(AppActivityTags.chatRoomsListAdapterTAG, roomName.toString() + " roomName clicked")

        }

        var roomName: TextView = itemView.findViewById(android.R.id.text1)

        init {
            itemView.setOnClickListener(this)

        }
    }

    interface RoomClickedInterface{
        fun roomSelected(room:Room)

    }
}

这是我的 ChatRoomActivity.kt

class ChatRoomActivity : AppCompatActivity() {
    lateinit var adapter:ChatRoomAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat_room)
        supportActionBar!!.title = intent.getStringExtra("room_name")
        adapter = ChatRoomAdapter()
        setUpRecyclerView()

        val currentUser = AppController.currentUser
        val roomId = intent.getStringExtra("room_id")

        currentUser.subscribeToRoomMultipart(
                roomId = roomId,
                listeners = RoomListeners(
                        onMultipartMessage = { message ->
                            Log.d("TAG",message.toString())
                            // com.pusher.chatkit.messages.multipart.Message

                            runOnUiThread(Runnable{
                                adapter.addMessage(message)
                                recycler_view.layoutManager?.scrollToPosition(adapter.itemCount -1)

                            })


                        },
                        onErrorOccurred = { error ->
                            Log.d(AppActivityTags.chatRoomActivityTAG, "error.reason: " + error.reason)
                            Log.d(AppActivityTags.chatRoomActivityTAG, "currentuser.rooms: " + currentUser.rooms)

                        }
                ),
                messageLimit = 100, // Optional
                callback = { subscription ->
                    // Called when the subscription has started.
                    // You should terminate the subscription with subscription.unsubscribe()
                    // when it is no longer needed
                }
        )

        button_send.setOnClickListener {
            if (edit_text.text.isNotEmpty()){
                currentUser.sendSimpleMessage(
                        roomId = roomId,
                        messageText = edit_text.text.toString(),
                        callback = { result -> //Result<Int, Error>
                            when (result) {

                                is Result.Success -> {
                                    runOnUiThread {
                                        edit_text.text.clear()
                                        hideKeyboard()
                                    }
                                }
                                is Result.Failure -> {
                                    Log.d(AppActivityTags.chatRoomActivityTAG, "error @ button_send.setOnclick: " + result.error.toString())
                                }
                            }
                        }
                )
            }
        }
    }

    private fun hideKeyboard() {
        val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        var view = this.currentFocus

        if (view == null) {
            view = View(this)
        }

        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    private fun setUpRecyclerView() {
        recycler_view.layoutManager= LinearLayoutManager(this@ChatRoomActivity)
        recycler_view.adapter = adapter
    }
}

这是我的 ChatRoomAdapter.kt

class ChatRoomAdapter: RecyclerView.Adapter<ChatRoomAdapter.ViewHolder>() {
    private var list = ArrayList<Message>()


    fun addMessage(message: Message){
        list.add(message)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.custom_chat_row,parent,false)

        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val inlineMessage: Payload.Inline = list[position].parts[0].payload as Payload.Inline
        holder.userName.text = list[position].sender.name
        holder.message.text = inlineMessage.content

    }


    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        var userName: TextView = itemView.findViewById(R.id.text_user_name)
        var message: TextView = itemView.findViewById(R.id.chat_message)

    }
}

推荐答案

我想我知道发生了什么.

I think I know what happened.

我认为发生了什么事,我是通过按钮聊天工具仪表板创建了一个房间,然后尝试以他们的身份登录.然后以他们的身份进入房间.我可以看到他们关联的聊天室列表,但是我认为,由于我是从仪表板创建聊天室的,因此认为我是其他人.

I think what happened was I created a room from the pusher-chat kit dashboard and then tried to sign in as them. and then enter the room as them. I was able to see my chatroomlists that they were affiliated with however I think that since I created the chatroom from the dashboard, it thought I was someone else.

长话短说,如果我通过android模拟器创建房间,然后转到房间,它就可以工作.如果我从仪表板创建房间并尝试加入,它似乎无法正常工作.

Long story short, it works if I create the room from my android emulator and then go to the room. if I create the room from the dashboard and try to join, it doesn't seem to work.

这篇关于Android Kotlin Pusher Chatkit-错误-需要房间会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆