AgoraIO / AgoraRTM_iOS

Agora's Real-time Messaging SDK Swift Package for iOS 📦
MIT License
5 stars 4 forks source link

AgoraRtmChannelDelegate is not called #5

Closed riverbaymark closed 2 years ago

riverbaymark commented 2 years ago

Greetings,

I ran this app demo and no members were showing up in the list after signing on. I took the code and modified it a bit for my own app for messaging and I can successful log into the rtm service and get a successful error response (code 0) when sending a message. The issue is the delegate is not called for either item. I am using the latest RTM version 1.4.10. Below is a sample of my code :

import SwiftUI
import AgoraRtmKit
import AgoraRtcKit

struct UserData: Codable {
    var rtmId: String
    var rtcId: UInt
    var username: String
    func toJSONString() throws -> String? {
        let jsonData = try JSONEncoder().encode(self)
        return String(data: jsonData, encoding: .utf8)
    }
}

class AgoraObservable: NSObject, ObservableObject {

    @Published var chatMessages: [ChatMessage] = []
    var channel: AgoraRtmChannel?
    @Published var channelName: String = "test"
    @Published var agoraSessionToken = "0061537aa75dc534ada816eb808742d17c4IAA1r7RGzcXe/ZnDKEeLOCNnUuD0O4WOpz+TQ0fC9Aoiawx+f9gAAAAAEAC3KIpZy8BuYgEAAQDLwG5i"
    @Published var username: String = ""
    @Published var members: [String] = []
    @Published var membersLookup: [String: (rtcId: UInt, username: String)] = [:] {
        didSet {
            members = self.membersLookup.values.compactMap {
                $0.username + (
                    $0.rtcId == self.rtcId ? " (Me)" : ""
                )
            }
        }
    }

    var rtcId: UInt = 0
    var rtmId = "test"
    var rtmIsLoggedIn = false

    lazy var rtckit: AgoraRtcEngineKit = {
        let engine = AgoraRtcEngineKit.sharedEngine(
            withAppId: "<#App ID#>", delegate: nil
        )
        engine.setChannelProfile(.liveBroadcasting)
        engine.setClientRole(.broadcaster)
        return engine
    }()

    lazy var rtmkit: AgoraRtmKit? = {
        let rtm = AgoraRtmKit(
            appId: "1537aa75dc534ada816eb808742d17c4", delegate: self
        )
        return rtm
    }()
}
extension AgoraObservable {
    func joinChannel() {
        if !self.rtmIsLoggedIn {
            rtmkit?.login(byToken: self.agoraSessionToken, user: self.rtmId) { loginResponse in
                if loginResponse != .ok {
                    fatalError("Could not log in to RTM")
                }
                print("Successfully logged into rtm")
                self.rtmIsLoggedIn = true
                self.joinChannel()
            }
            return
        }

            self.channel = self.rtmkit?.createChannel(withId: self.channelName, delegate: self)
            self.channel?.join(completion: { joinStatus in
                if joinStatus == .channelErrorOk {
                    print("Successfully joined channel")
                    print(self.channel)

                } else {

                    print(joinStatus)

                    self.channel = nil

                }
            })
    }
}

extension AgoraObservable: AgoraRtmChannelDelegate, AgoraRtmDelegate {
    func channel(_ channel: AgoraRtmChannel, memberJoined member: AgoraRtmMember) {
        print("Agora member: \(member.userId)")
    }

    func channel(_ channel: AgoraRtmChannel, messageReceived message: AgoraRtmMessage, from member: AgoraRtmMember) {
        print("Message received from: \(member.userId)")

        print(message)

        var color = Color.gray
        var isMe = false
        if member.userId == UIDevice.current.name {
            color = Color.blue
            isMe = true
        }
        let newMessage = ChatMessage(message: message.text, avatar: member.userId, color: color, isMe: isMe)

        chatMessages.append(newMessage)

        //parseMemberData(from: message.text)
    }
    func rtmKit(_ kit: AgoraRtmKit, messageReceived message: AgoraRtmMessage, fromPeer peerId: String) {
        parseMemberData(from: message.text)
    }

    func parseMemberData(from text: String) {
        guard let textData = text.data(using: .utf8),
              let decodedUserData = try? JSONDecoder().decode(UserData.self, from: textData)
        else {
            return
        }
        membersLookup[decodedUserData.rtmId] = (decodedUserData.rtcId, decodedUserData.username)
    }
    func channel(_ channel: AgoraRtmChannel, memberLeft member: AgoraRtmMember) {
        membersLookup.removeValue(forKey: member.userId)
    }
}

Any help would be greatly appreciated!

plutoless commented 2 years ago

hi @riverbaymark im not seeing you calling sendMessage api, did you miss that?

riverbaymark commented 2 years ago

Sorry I left that code out int the above snippet. I call it from my SwiftUi view like below:

func sendData() { print(agoraChatOO.channel) agoraChatOO.channel?.send(AgoraRtmMessage(text: dataTextSending)){message in print(message) } }

agoraChatOO is the @StateObject view model for the AgoraObservable class.

plutoless commented 2 years ago

@riverbaymark is there any chance you can provide the minimal reproducible project to me? it seems agoraChat00.channel? can potentially be nil? either you can put everything together or you may provide the reproducible project can help me solve your problem quicker.

riverbaymark commented 2 years ago

Here is a shortened example of my view that utilizes the observable object above. I do check with the print statement in the sendData() function and the agoraChatOO.channel is not nil and is the same as when created in the observable object class.

 import SwiftUI
 import AgoraRtmKit

 struct AgoraChatView: View {
     @StateObject var agoraChatOO = AgoraObservable()
     @State var dataTextSending: String = ""
     @State var joinedChannel: Bool = false

     func sendData() {
         print(agoraChatOO.channel)
         agoraChatOO.channel?.send(AgoraRtmMessage(text: dataTextSending)){message in
             print(message)

         }
     }

     var body: some View {
         GeometryReader {
             geometry in
             VStack{
                 Button(action: {
                     joinedChannel.toggle()
                     if !joinedChannel {
                         self.agoraChatOO.members.removeAll()
                         self.agoraChatOO.rtckit.leaveChannel()
                         self.agoraChatOO.rtmkit?.logout()
                         self.agoraChatOO.rtmIsLoggedIn = false
                     } else {
                         self.agoraChatOO.joinChannel()
                     }
                 }, label: {
                     Text("\(joinedChannel ? "Leave" : "Join") Channel")
                         .accentColor(joinedChannel ? .red : .blue)
                 })
                 HStack {
                     TextField("Message", text: $dataTextSending)
                         .padding()
                         .textFieldStyle(RoundedBorderTextFieldStyle())
                     Spacer()
                     Button(action: {
                         self.sendData()
                         dataTextSending = ""
                     }) {
                         Text("Send")
                             .padding(8)
                             .background(Color.blue)
                             .foregroundColor(.white)
                             .cornerRadius(8)
                     }
                 }.navigationBarTitle("RadChat")
             }
         }
     }
 }

 struct AgoraChatView_Previews: PreviewProvider {
     static var previews: some View {
         AgoraChatView(agoraChatOO: AgoraObservable())
     }
 }
plutoless commented 2 years ago

@riverbaymark can you listen to the connectionStateChange callback and see what you get for both users?

maxxfrazer commented 2 years ago

@riverbaymark are the two users logging in with the same rtmId of "test"? They cannot both be the same value. I usually use UIDevice.current.identifierForVendor?.uuidString. Note that it will be extremely unlikely to be nil, but add a fallback random UUID just in case.

maxxfrazer commented 2 years ago

I think this issue was solved by changing the RTM ID.