IOSsdk_version: v11.4.2
用户A 向用户B 发送第一条消息时,B端可以被正常接收。但A发送第二条消息时,B端就没动静了。
接收代码:
// client在打开app时建立
// 在VC viewDidLoad时,client.open{}
override func viewDidLoad() {
super.viewDidLoad()
self.dataModel.assetsClient?.open(callback: {suc, error in
if error != nil {
print("未能打开即时通讯服务 \(error?.localizedDescription ?? "..")")
}
})
})
}
// 代理部分
func conversation(_ conversation: AVIMConversation, didReceiveCommonMessage message: AVIMMessage) {
print("conversation did receive common message \(message.content ?? "..")")
guard let content = message.content else {return}
switch content {
....
}
}
发送相关代码
/**
创建交流数据组。数据名字"asset+uid"
*/
func createAssetsConversation(user: UserData, clinet: AVIMClient, otherUid: String, completion: @escaping ((AVIMConversation?)->())){
let clientIds = [clinet.clientId, otherUid]
let attributes = ["senderId" : user.uid,
"senderName" : user.name,
"otherUid": otherUid]
let options = AVIMConversationOption.unique
clinet.createConversation(withName: "assets+\(user.uid)", clientIds: clientIds, attributes: attributes as [AnyHashable : Any], options: options) { (converstaion, error) in
completion(converstaion)
}
}
/**
获取conversation
*/
func fetchAssetsConversation(client: AVIMClient, otherUid: String, completion: @escaping ((AVIMConversation?)->())){
let query = client.conversationQuery()
//query.cachePolicy = AVIMCachePolicy.ignoreCache
query.whereKey("attr.otherUid", equalTo: otherUid)
query.findConversations(callback: {(conversations, error) in
guard let first = conversations?.first else {
print("没有查到与用户\(otherUid)的交流")
completion(nil)
return
}
completion(first)
})
}
func sendAsstesInformation(conversation: AVIMConversation ,assetMessage: String, completion: @escaping ((Bool)->())){
let message = AVIMMessage(content: assetMessage)
conversation.send(message, callback: {
suc, error in
if error != nil {
print("发送消息 '\(assetMessage)' 失败 \(error?.localizedDescription ?? "...")")
}
completion(suc)
})
}
func sendAssetsMessageFromCurrentUserToOhterUser(currentUser: UserData, clinet: AVIMClient, otherUid: String, assetInfo: String, completion: @escaping ((Bool)->())){
fetchAssetsConversation(client: clinet, otherUid: otherUid, completion: {
conversation in
if let con = conversation {
sendAsstesInformation(conversation: con, assetMessage: assetInfo, completion: { suc in
completion(suc)
})
}else {
createAssetsConversation(user: currentUser, clinet: clinet, otherUid: otherUid, completion: {newC in
if let newCon = newC {
sendAsstesInformation(conversation: newCon, assetMessage: assetInfo, completion: {suc in
completion(suc)
})
}
})
}
})
}
// 发送消息
//获取对方数据成功后向对方发送消息
takeDataFromUser(uid: loserId, selfProfile: profile, takeAmount: g_throwinPrize, isSucceed: {
isSucceed in
if isSucceed == true {
/*******
此处开始发送消息
*******/
data.clinet.open{ suc, error in
if error != nil {
print("无法向对方发送消息 \(error?.localizedDescription ?? "..")")
}
// 向被发通知
sendAssetsMessageFromCurrentUserToOhterUser(currentUser: data.currentUser, clinet: data.clinet, otherUid: loserId, assetInfo: UserChosedPrize.throwinRate.rawValue, completion: {suc in
if suc == true {
print("消息发送成功")
}
})
}
}
})
以上代码,用户打卡app后第一次发送,发送接收都没问题。但用户A第二次发送后,发送成功,但接收没有反映。
然后我检查了操作台的即时通讯部分。发现。除了第一次外,后面B端所有接收的消息,都显示为”未读“。并且显示为”离线“。但用户B明明是在线状态。
请问以上问题怎么造成的?应该如何修改?非常感谢。
(ps: 我试着在控制台发送系统对话,结果是一样的)