Quickblox: how find out whether the user is online?

点点圈 提交于 2020-01-02 15:48:33

问题


Is there any way to find out whether the Quickblox user is online? I'm using Quickblox iOS SDK.


回答1:


There are 2 ways:

  1. Using REST API - here is a guide http://quickblox.com/developers/SimpleSample-users-ios#Online.5COffline_status
  2. Using Chat contact list http://quickblox.com/developers/SimpleSample-chat_users-ios#Contact_list



回答2:


Swift 5 :

To get opponent user's Online Status And accurate Last Seen we have to add both users in each others contactList by using addUserToContactListRequest() API.


User A sends a request to become "friends" with user B. User B accepts the friend request. And now user A and B appear in each other's roster.


Step 1:

Check whether opponent user is already added in contacts or not

 let isAvailable = QBChat.instance.contactList?.contacts?.filter {(contacts) -> Bool in

    // self.dialog.recipientID is an opponent user's ID 

    return contacts.userID == self.dialog.recipientID ? true : false
}

If available in contacts check online status, if not available then send request to add in contacts.

    if isAvailable!.count > 0 {
         if isAvailable![0].isOnline {
            //print("User Is Online")
         } else {
            //print("User Is Offline")
            //Check Last Seen
            self.fetchLastSeen(userId: self.dialog.recipientID)
         }
    } else {
           QBChat.instance.addUser(toContactListRequest: UInt(self.dialog!.recipientID)) { (err) in
                    print("\(err)")
                }
  }

Step 2 :

implement QBChatDelegate methods.

Add contacts request

// This method will get called whenever you will receive any new add contact request
    func chatDidReceiveContactAddRequest(fromUser userID: UInt) {
        //Confirm add to contact list request.    
        QBChat.instance.confirmAddContactRequest(userID) { (error) in
        }
    }

Following method called in case when user's from contact list online status has been changed.

func chatDidReceiveContactItemActivity(_ userID: UInt, isOnline: Bool, status: String?) {
    if userID == self.dialog.recipientID {
        if isOnline {
            print("User Is Online")
         } else {
            //print("User Is Offline")
            //Check Last Seen
            fetchLastSeen(userId: NSInteger(userID)) 
        }
    }
}

Step 3:

fetch the last seen of an user using this lastActivityForUser()

func fetchLastSeen(userId: NSInteger){
    QBChat.instance.lastActivityForUser(withID: UInt(userId)) { (timeStamp, err) in
        print(timeStamp)
       // here we get total seconds, since how long user was inactive 
       // minus the seconds from current time 
        if err == nil {
            let updatedTime = Calendar.current.date(byAdding: .second, value: -Int(timeStamp), to: Date())

            guard let dateSent = updatedTime else {
                return
            }

            var lastSeenStr = ""
            if (Calendar.current.isDateInToday(updatedTime!)){
                lastSeenStr = "Today"
            } else if (Calendar.current.isDateInYesterday(updatedTime!)){
                lastSeenStr = "Yesterday"
            } else {
                let dateFormat = DateFormatter()
                dateFormat.dateFormat = "d-MMM"
                lastSeenStr = dateFormat.string(from: updatedTime!)
            }

            let text = messageTimeDateFormatter.string(from: dateSent)
            print("\(lastSeenStr + " " + text)") // e.g. 11-Sep 11:44 AM
        }
    }
}


来源:https://stackoverflow.com/questions/17962614/quickblox-how-find-out-whether-the-user-is-online

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!