【问题标题】:Play Streaming URL in NotificationService in ios swift在 ios swift 中的 NotificationService 中播放流式 URL
【发布时间】:2019-07-18 11:56:51
【问题描述】:

我通过保存在磁盘中的通知服务扩展中播放了视频。现在我想在通知服务扩展中播放流 URL 作为附件。我曾尝试直接将 URL 作为附件传递,但它在变量 attach1 中返回 nil。

下面是我的代码:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift unnotificationserviceextension


    【解决方案1】:

    您必须了解一些场景。

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

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

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

    我不确定,但是,youtube 视频 url 链接无法在 AVPlayer 中播放,与 AVPlayer 一起使用时,应该需要在 url 中扩展视频。

    正确检查所有内容。请参阅UNNotificationServiceExtensionUNNotificationContentExtension 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多