首页 > 解决方案 > 如何在 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())
            }
        }
    }

标签: iosswiftapple-push-notificationstwiliotwilio-programmable-chat

解决方案


推荐阅读