我正在尝试使用 SendBird SDK 在我的 iOS 应用程序中实现一个简单的即时消息功能。
我已阅读快速入门、身份验证和消息传递通道 - 基本指南,并尝试按照他们的描述设置 1-1 消息传递通道。
在application:didFinishLaunchingWithOptions
我包括SendBird.initAppId("MyAppID")
.
在我的初始视图控制器中,我登录了用户。我使用我的应用程序中的用户用户名作为他的 userId,并将他的名字和姓氏连接起来用作他的 SendBird 昵称:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let emailAddress = KCSUser.activeUser().email,
firstName = KCSUser.activeUser().givenName,
lastName = KCSUser.activeUser().surname {
let nickname = firstName.lowercaseString + lastName.lowercaseString
SendBird.loginWithUserId(emailAddress, andUserName: nickname)
}
}
最后,在实际的聊天视图控制器中,我尝试与另一个用户启动消息通道:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
guard let targetId = session?.parent?.user?.email else {
return
}
// I logged in once with this id, so according to the SendBird docs, the
// user should exist.
SendBird.startMessagingWithUserId(targetId)
SendBird.setEventHandlerConnectBlock({ (channel) in
NSLog("%@", channel)
}, errorBlock: { (errorCode) in
NSLog("%D", errorCode)
}, channelLeftBlock: { (channel) in
}, messageReceivedBlock: { (message) in
}, systemMessageReceivedBlock: { (systemMessage) in
}, broadcastMessageReceivedBlock: { (broadcastMessage) in
}, fileReceivedBlock: { (fileLink) in
}, messagingStartedBlock: { (messagingChannel) in
SendBird.joinChannel(messagingChannel.getUrl())
// SendBirdChannelInfo is a custom struct for some use within the app
let channelInfo = SendBirdChannelInfo(
channelId: messagingChannel.getId(),
url: messagingChannel.getUrl()
)
// self.session is a custom data model for use with our database.
self.session?.sendBirdChannelInfo = channelInfo
SendBird.queryMessageListInChannel(messagingChannel.getUrl()).prevWithMessageTs(
Int64.max, andLimit: 30, resultBlock: { (queryResult) in
var maxMessageTs = Int64.min
for model in queryResult {
if maxMessageTs <= (model as! SendBirdMessageModel).getMessageTimestamp() {
maxMessageTs = (model as! SendBirdMessageModel).getMessageTimestamp()
}
}
SendBird.connectWithMessageTs(maxMessageTs)
}, endBlock: { (error) in
if let fetchMessagesError = error {
NSLog(fetchMessagesError.localizedDescription)
}
})
}, messagingUpdatedBlock: { (messagingChannel) in
}, messagingEndedBlock: { (messagingChannel) in
}, allMessagingEndedBlock: {
}, messagingHiddenBlock: { (messagingChannel) in
}, allMessagingHiddenBlock: {
}, readReceivedBlock: { (readStatus) in
}, typeStartReceivedBlock: { (typeStatus) in
}, typeEndReceivedBlock: { (typeStatus) in
}, allDataReceivedBlock: { (unsignedInt, thirtyTwoBitInt) in
}, messageDeliveryBlock: { (sent, message, data, messageId) in
}, mutedMessagesReceivedBlock: { (message) in
}) { (fileLink) in
}
}
除了我评论的那几行,这段代码直接来自 SendBird 手册。但是,当它运行时,我收到错误代码 14000,并记录了消息“无法启动消息传递”。
错误的真正原因是什么?我是在用户登录或 SDK 初始化过程中遗漏了一个步骤,还是在创建频道时还有另一个步骤?还是完全是别的东西?