Quickblox:如何确定用户是否在线?
有没有办法确定Quickblox用户是否在线? 我正在使用Quickblox iOS SDK。有两种方法:Quickblox:如何确定用户是否在线?,quickblox,Quickblox,有没有办法确定Quickblox用户是否在线? 我正在使用Quickblox iOS SDK。有两种方法: 使用RESTAPI-以下是一个指南 使用聊天联系人列表 Swift 5: 要获得对手用户的在线状态和准确的上次访问,我们必须使用addUserToContactListRequest()API将这两个用户添加到彼此的联系人列表中 用户A发送与用户B成为“朋友”的请求。用户B接受好友请求。现在用户A和B出现在对方的名册上 步骤1: 检查对方用户是否已添加到联系人中 let isAvai
addUserToContactListRequest()
API将这两个用户添加到彼此的联系人列表中
用户A发送与用户B成为“朋友”的请求。用户B接受好友请求。现在用户A和B出现在对方的名册上
步骤1: 检查对方用户是否已添加到联系人中
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 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)")
}
}
步骤2:
实施QBChatDelegate
方法
添加联系人请求
// 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
}
}
当用户的联系人列表在线状态发生更改时,调用以下方法
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))
}
}
}
步骤3:
使用此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
}
}
}