Sharing data from a notification service extension

I added the Notification Service Extension and Content Extension to my existing app. The service extension downloads the video file using the URL passed in the APNS push notification. Then it is attached to the notification. The content extension passes the app url to the AVPlayer, so the video plays inside the notification if the user chooses to watch it. So far, so good. Now I would like to save the downloaded video files for viewing in the main application, as a table. To do this, I created an application group, enabled the application group capabilities for both the main purpose and the content service extension, and selected the application group under this capability, again for both. Then, instead of saving the file to the temp directory in the service extension, I moved the downloaded file to the shared application container:

let groupDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.net.mydomain.myapp")!
let groupPathUrl = groupDirectoryUrl.appendingPathComponent("Library/Caches/myapp") // added this later to see if it would work from within the Caches directory
let groupPath = groupPathUrl.path

// Code snipped, creating myapp folder if it does not exist

do {
    try FileManager.default.moveItem(at: location, to: groupFileUrl) // location is the url of the downloaded file
    self.tmpLogger.addLogEntry(severity: 5, "<File copied to>: \(groupFileUrl)") // This is so I can view log entries from within my app
    if (FileManager.default.fileExists(atPath: groupFileUrl.path)) { // Temporary code to double check the file existence
        self.tmpLogger.addLogEntry(severity: 5, "<File now exists>: " + groupFileUrl.path)
    }
} catch let error {
    bestAttemptContent.title = "\(bestAttemptContent.title) <FILE MOVE ERR>" // So the error will be visible in the notification, for debug
    bestAttemptContent.body = "\(bestAttemptContent.body) \(error.localizedDescription)"
    self.tmpLogger.addLogEntry(severity: 1, "<File move error>: \(error.localizedDescription)")
}

      

No problem, moving the file works. But here's the thing, it is not visible from the main application. In a table view controller:

let documentDirectoryUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.net.mydomain.myapp")!
let fileNameOnly = NSString(string: mediaEntry.fileName!).lastPathComponent //mediaEntry is CoreData, containing the url of the saved file. CoreData works
let storagePathUrl = documentDirectoryUrl.appendingPathComponent("Library/Caches/myapp")

let fileName = storagePathUrl.absoluteString.appending(fileNameOnly)
let url = URL(string: fileName)!

MyLogger().addLogEntry(severity: 5, "Filename: " + url.path)
if (FileManager.default.fileExists(atPath: url.path)) {
    // This part of the code is never reached, the file never exists

      

I have updated the table view code to reload the video file if it doesn't exist in the shared container and save it in the same location. Now that it is now, from the main application, when a video message is selected, the video is downloaded and saved to the shared container and can then be viewed. This will persist, so it can be viewed many times, but it will only be loaded once.

I have added code to display content from a notification content extension. Files that are loaded in the main application do not appear in the extension. Thus, although there are no errors when accessing the shared container, both programs cannot see each other's data.

I know there is at least one app extension that is not allowed to access shared data, but have not found any information that this is specific to the notification service. Any thoughts?

+3


source to share





All Articles