我通过保存在磁盘中播放了通知服务扩展中的视频。现在我想在通知服务扩展中播放流 URL 作为附件。我尝试直接将 URL 作为附件传递,但它在变量中返回 nil attach1
。
下面是我的代码:
import UserNotifications
import UIKit
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
//Media
func failEarly() {
contentHandler(request.content)
}
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return failEarly()
}
guard let attachmentURL = content.userInfo["attachment_url"] as? String, let url = URL(string: attachmentURL) else {
return failEarly()
}
// Saving streaming url video
var attach1 : UNNotificationAttachment?
do {
attach1 = try UNNotificationAttachment(identifier: request.content.categoryIdentifier, url: url, options: nil)
} catch {
failEarly()
}
content.attachments = [attach1] as! [UNNotificationAttachment]
contentHandler(content.copy() as! UNNotificationContent)
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
我的流媒体 URL 来自Youtube。
任何帮助将不胜感激。