Rich Push通知视频附件

时间:2019-06-20 17:53:05

标签: swift

我正在尝试显示带有丰富推送通知的视频。我的图片/ gif工作正常,但视频似乎出现了问题,因为它仅显示文本 这是我的代码

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)



    func failEarly() {
        contentHandler(request.content)
    }

    guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
        return failEarly()
    }

    guard let apnsData = content.userInfo["data"] as? [String: Any] else {
        return failEarly()
    }

    guard let attachmentURL = apnsData["attachment-url"] as? String else {
        return failEarly()
    }

    if !attachmentURL.contains(".jpg") {
        let task = URLSession.shared.downloadTask(with: URL(string: attachmentURL)!) { (downloadedUrl, response, error) in

            guard let downloadedUrl = downloadedUrl else {
                return
            }

            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

            var url = URL(fileURLWithPath: path)
            url = url.appendingPathComponent("video.mp4")

            try? FileManager.default.moveItem(at: downloadedUrl, to: url)

            do {
                let attachment = try UNNotificationAttachment(identifier: "video", url: url, options: nil)
                do {
                    content.attachments = [attachment]
                    contentHandler(content.copy() as! UNNotificationContent)
                }
            }
            catch {

            }
        }
        task.resume()
    } else { //this is for image/gif works fine
        guard let imageData = NSData(contentsOf:NSURL(string: attachmentURL)! as URL) else { return failEarly() }
        guard let attachment = UNNotificationAttachment.create(imageFileIdentifier: "image.gif", data: imageData, options: nil) else { return failEarly() }

        content.attachments = [attachment]
        contentHandler(content.copy() as! UNNotificationContent)
    }

}

我甚至尝试对小于1mb的视频进行硬编码的url。 因此,如果您在代码中发现任何错误,请告诉我 因为只有图片似乎有效

0 个答案:

没有答案