ios - 如何在 ios swift 的 twilio 聊天推送通知有效负载中添加额外参数
问题描述
我正在开发 Twilio 可编程聊天服务。
如何在 twilio 聊天消息发送时传递额外参数?
我正在使用 attributes 属性来添加额外的参数。另一点是有时我会收到聊天通知,有时没有收到,在我的 twilio 仪表板上我确实看到了这种类型的错误错误 - 52143 推送通知被 APNs 拒绝
而且在我的聊天屏幕中收到任何媒体文件时,我从未收到过聊天推送通知
let messageOptions = TCHMessageOptions().withBody(inputMessage)
let extraParam = JSON(["booking_id": "100"]).rawString(String.Encoding.utf8, options: JSONSerialization.WritingOptions.fragmentsAllowed)!
messageOptions.withAttributes(["attributes": extraParam]) { (result) in
self.channel.messages?.sendMessage(with: messageOptions, completion: nil)
}
用于发送媒体文件
func sendImageInMessage(image: UIImage, fileExtension: String){
// guard let imageSelected = image else { return }
if let imageSizeInfo = image.getFileSize() {
print("\(String(describing: size)), \(type(of: size))") // 51.9, Double
if imageSizeInfo <= 3.0 {//check of image size is 3 MB or lessthan 3 MB
Utility.shared.showSVProgressLoaderWithMessage(loaderMessage: "Uploading...")
// The data for the image you would like to send
let fileName = String(format: "%@_%@.%@", (AppDelegate.sharedInstance.userInfoData?.data?.id)!, Date().toString(format: "yyyyMMddHHmmss"), fileExtension)
var data : Data {
if(fileExtension == "png"){
return image.pngData()!
}
return image.jpegData(compressionQuality: 0.5)!
}
let imageData = data
// Prepare the upload stream and parameters
let messageOptions = TCHMessageOptions().withBody("attachment")
let inputStream = InputStream(data: imageData)
messageOptions.withMediaStream(inputStream,
contentType: "image/\(fileExtension)",
defaultFilename: fileName,
onStarted: {
// Called when upload of media begins.
print("Media upload started")
},
onProgress: { (bytes) in
// Called as upload progresses, with the current byte count.
print("Media upload progress: \(bytes)")
}) { (mediaSid) in
// Called when upload is completed, with the new mediaSid if successful.
// Full failure details will be provided through sendMessage's completion.
print("Media upload completed")
self.manageUSedCountToUserDefault(usedWords: 30)
}
// Trigger the sending of the message.
self.channel.messages?.sendMessage(with: messageOptions, completion: { (result, message) in
Utility.shared.dismissSVProgressLoader()
if !result.isSuccessful() {
print("Creation failed: \(String(describing: result.error))")
} else {
print("Creation successful")
guard let directoryURL = TwilioChatManager.shared.downloadDirectoryURL else {
return
}
let filePath = String(directoryURL.appendingPathComponent(fileName).absoluteString.dropFirst(7))
FileManager.default.createFile(atPath: filePath, contents: imageData, attributes: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
let section = self.tableView.numberOfSections - 1
let row = self.tableView.numberOfRows(inSection: section) - 1
self.tableView.reloadRows(at: [IndexPath(row: row, section: section)], with: .none)
}
}
})
}else{ // Image is greter than 3 MB
Utility.shared.showAlert(title: "", message: "IMAGE_IS_LARGER_THAN_3_MB".localized())
}
}
}
解决方案
推荐阅读
- android - 应用程序适用于 android 7 和 8,但不适用于 android 5 和 6
- react-native - 无法从“node_modules/react-native-router-flux/dist/Router.js”解析“mobx-react/native”
- python-3.x - 尽管子文件夹已添加到搜索路径,但未找到子文件夹中的模块
- android - 房间数据库,inMemoryDatabaseBuilder和databaseBuilder有什么区别?
- c++ - 同名函数的C++编译错误
- variables - Visual Studio Code Snippet Variable Transform not working
- python - h2o 中跨行的方差
- angular - Angular Material2 菜单内的滑动切换
- html - Chrome css 过渡:带有过滤器的文本消失
- telerik - Sitefinity 11无法在前端小部件控件的回发中获取下拉选择值