Let's say you want to implement a simple chat bubble like this, with 'UIKit framework' and Swift:
When sending a message, fill in the key-value data in 'metaData' property which will be used for creating an UIView instance.
import UIKit
import TalkPlus
func sendCardViewMessage() {
guard let channel = <YOUR_TALK_PLUS_CHANNEL> else { return }
// fill in the key-value data in 'metaData' property
let params = TPMessageSendParams(contentType: .text,
messageType: .hidden,
channel: channel)
params?.metaData = [
"type": "msg_with_button",
"title": "Hi!",
"body": "This is a simple chat bubble with a button.",
"button_text": "Say Hello",
"button_action": "SayHello"
]
// Send the message to the channel
TalkPlus.sharedInstance()?.sendMessage(params, success: { message in
// succeeded in sending the message
}, failure: { errorCode, error in
// failed in sending message.
})
}
You can enter up to 5 key-value pairs in data field. The maximum size of key is 128 characters and the maximum size of value is 1024 characters. Both key and value must be strings.
Define a 'CardViewMessageModel' class that contains data to be displayed on the screen.
'messageReceived' method will be called whenever receiving a new message. At this method, call 'getData' method in 'TPMessage to get key-value data and then create an instance of 'CardViewMessageModel'.
extension ViewController: TPChannelDelegate {
...
// Called when receiving a new message from the chat channel
func messageReceived(_ tpChannel: TPChannel!, message tpMessage: TPMessage!) {
// Create an instance of 'CardViewMessageModel'
guard let data = tpMessage.getData() as? [String : Any],
data.count > 0 else { return }
// Decode key-value data, and then create a new
guard let cvm = dictionaryToClass(data,
type: CardViewMessageModel.self) else { return }
// Creates a view and add a view to where you want.
CardViewMessageModel.addCardView(cvm: cvm, parentView: self.view)
}
...
}
As a final step, you need to create a view and add it where you want. In the above example, 'CardViewMessageModel.addCardView' method creates a view and adds the created view to the parent view.