【问题标题】:How to delete files in iOS Notification Service Extension?如何删除 iOS 通知服务扩展中的文件?
【发布时间】:2019-03-28 22:08:25
【问题描述】:

我有一个UNNotificationServiceExtension,它将视频和图像下载到Documents 目录,供采用UNNotificationContentExtension 的类使用。我想删除任何通知不再使用的媒体文件。我不知道该怎么做。

  • 我尝试删除 AppDelegate 中的文件,但我相信 UNNotificationServiceExtension 有自己的 Documents 目录,根据本文档的“与包含的应用程序共享数据”部分:https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html,所以我无法访问这些文件来自我的主应用程序。它们位于不同的容器中。
  • 我不想创建一个应用组来在应用和扩展程序之间共享数据,只是为了删除未使用的文件。
  • 我不想删除UNNotificationServiceExtension 中未使用的文件,因为扩展程序完成工作的时间有限,如果我尝试下载文件并删除其他文件,可能会时间出去。

我认为最好的选择是检查任何传递的通知需要哪些文件,并删除通知服务扩展的Documents 目录中不需要的文件。我对此的担忧是,UNNotificationServiceExtension 只给了很短的时间,在此期间它必须完成所有工作,之后它将超时。

所以,我的问题是,“这是从通知服务扩展中清理未使用文件的正确方法,还是有更好的方法?”

【问题讨论】:

  • 当您从设备的通知托盘中清除通知时,设备会自动删除随通知下载的媒体(通知内容)。如果您明确地将数据存储在其他地方,那么您必须添加应用程序组才能存储和删除数据。

标签: ios unnotificationserviceextension


【解决方案1】:

感谢 manishsharma93,我能够实施一个很好的解决方案。我现在将文件存储在主应用程序和通知服务扩展共享的目录中。我首先必须使用此处找到的信息设置一个共享应用组:https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW19

然后在我的 AppDelegate 中,我添加了这个私有函数,我在 applicationDidFinishLaunching(_:) 方法的末尾调用它:

// I call this at the end of the AppDelegate.applicationDidFinishLaunching(_:) method
private func clearNotificationMedia() {
    // Check to see if there are any delivered notifications. If there are, don't delete the media yet,
    // because the notifications may be using them. If you wanted to be more fine-grained here,
    // you could individually check to see which files the notifications are using, and delete everything else.
    UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
        guard notifications.isEmpty else { return }

        let fileManager = FileManager.default

        guard let mediaCacheUrl = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourGroupHere")?.appendingPathComponent("media_cache", isDirectory: true) else { return }

        // Check to see if the directory exists. If it doesn't, we have nothing to do here.
        var isDirectory: ObjCBool = false
        let directoryExists = FileManager.default.fileExists(atPath: mediaCacheUrl.path, isDirectory: &isDirectory)
        guard directoryExists && isDirectory.boolValue else {
            print("No media_cache directory to delete.", terminator: "\n")
            return
        }

        // The directory exists and there aren't any notifications using media stored there,
        // so go ahead and delete it. Use a lock to make sure that there isn't data corruption,
        // since the directory is shared.
        let lock = NSLock()
        lock.lock()
        do {
            try FileManager.default.removeItem(at: mediaCacheUrl)
            DebugLog("Successfully deleted media_cache directory.")
        } catch let error as NSError {
            DebugLog("Error: \(error.localizedDescription). Failed to delete media_cache directory.")
        }
        lock.unlock()
    }
}

它就像一个魅力。再次感谢您为我指明正确的方向 manishsharma93。

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2020-02-20
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多