0

我通过保存在磁盘中播放了通知服务扩展中的视频。现在我想在通知服务扩展中播放流 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

任何帮助将不胜感激。

4

1 回答 1

1

有些场景你必须知道。

  1. 如果您使用内容扩展名,则该时间category必须与.Info.plist文件中声明的时间相同UNNotificationContentExtension
  2. UserNotificationAttachment无法存储数据 url。即它必须包含fileUrl,它是下载的文件的url,UNNotificationServiceExtension并在完成时返回。

所以,你下面的代码是完全错误的。

var attach1 : UNNotificationAttachment?
do {
    attach1 = try UNNotificationAttachment(identifier: request.content.categoryIdentifier, url: url, options: nil)
} catch {
    failEarly()
}    

我不确定,但是 youtube 视频 url 链接无法在 中播放AVPlayer,与AVPlayer.

正确检查所有的东西。有关详细信息,请参阅UNNotificationServiceExtensionUNNotificationContentExtension

于 2019-07-18T13:27:37.650 回答