1

I am working on Twilio Programmable Chat Service.

How can i pass the extra parameter at twilio chat message sending time?

I am using attributes property for adding extra parameter. And another points is sometimes i will received the chat notification and sometimes not received and on my twilio dashboard i did see this type of error Error - 52143 The push notification was rejected by APNs

And I am never received chat push notification when received any media files in my chat screen

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)
}

For sending media files

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())
            }
        }
    }

0 Answers0