From cd9c4d4c58b33adee6d3b8705f471e4b5848333e Mon Sep 17 00:00:00 2001 From: Chris Li Date: Tue, 20 Sep 2016 11:25:56 -0400 Subject: [PATCH] Download Notification --- Kiwix-iOS/AppDelegate.swift | 19 +++----- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOS/Notifications.swift | 4 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- .../xcdebugger/Breakpoints_v2.xcbkptlist | 16 ------- Kiwix/Network/Network.swift | 45 ++++++++++++++++--- 6 files changed, 50 insertions(+), 38 deletions(-) diff --git a/Kiwix-iOS/AppDelegate.swift b/Kiwix-iOS/AppDelegate.swift index 0a16f9ff..efdaecc2 100644 --- a/Kiwix-iOS/AppDelegate.swift +++ b/Kiwix-iOS/AppDelegate.swift @@ -8,6 +8,7 @@ import UIKit import CoreData import Operations +import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -24,8 +25,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func registerNotification() { - let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) - UIApplication.sharedApplication().registerUserNotificationSettings(settings) + if #available(iOS 10, *) { + UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Badge, .Sound], completionHandler: { _ in }) + } else { + let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) + UIApplication.sharedApplication().registerUserNotificationSettings(settings) + } } // MARK: - @@ -144,16 +149,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - - if #available(iOS 10, *) { - - } else { - let notification = UILocalNotification() - notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished") - notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished") - notification.soundName = UILocalNotificationDefaultSoundName - UIApplication.sharedApplication().presentLocalNotificationNow(notification) - } } func applicationWillEnterForeground(application: UIApplication) { diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 34e2a881..bacca021 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.873 + 1.8.900 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/Notifications.swift b/Kiwix-iOS/Notifications.swift index d0313bf7..866f6c8f 100644 --- a/Kiwix-iOS/Notifications.swift +++ b/Kiwix-iOS/Notifications.swift @@ -8,6 +8,8 @@ import UIKit -class DownloadFinishedNotifications: { +class DownloadFinishedNotifications: UILocalNotification { } + +//@available(iOS 10, *) diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index b52185b4..7cfa5662 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.876 + 1.8.903 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 55d0ddea..dbfe494b 100644 --- a/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Kiwix.xcworkspace/xcuserdata/chrisli.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -19,21 +19,5 @@ landmarkType = "5"> - - - - diff --git a/Kiwix/Network/Network.swift b/Kiwix/Network/Network.swift index 6787b207..5c3d0bac 100644 --- a/Kiwix/Network/Network.swift +++ b/Kiwix/Network/Network.swift @@ -9,13 +9,14 @@ import UIKit import CoreData import Operations +import UserNotifications class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate, OperationQueueDelegate { static let shared = Network() let queue = OperationQueue() let context = NSManagedObjectContext.mainQueueContext - private(set) var operations = [String: DownloadBookOperation]() + private(set) var operations = [String: DownloadBookOperation]() private var downloadedBookTitle = [String]() private var completionHandler: (()-> Void)? @@ -60,13 +61,42 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe NSOperationQueue.mainQueue().addOperationWithBlock { self.completionHandler?() - let notification = UILocalNotification() - notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished") - notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished") - notification.soundName = UILocalNotificationDefaultSoundName - UIApplication.sharedApplication().presentLocalNotificationNow(notification) + let title = NSLocalizedString("Book download finished", comment: "Notification: Book download finished") + let body: String = { + switch self.downloadedBookTitle.count { + case 0: + return NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished") + case 1: + return String(format: NSLocalizedString("%@ has been downloaded", comment: "Notification: Book download finished"), self.downloadedBookTitle[0]) + case 2: + return String(format: NSLocalizedString("%@ and @% have been downloaded", comment: "Notification: Book download finished"), + self.downloadedBookTitle[0], self.downloadedBookTitle[1]) + default: + return String(format: NSLocalizedString("%@ and %d others have been downloaded", comment: "Notification: Book download finished"), + self.downloadedBookTitle[0], self.downloadedBookTitle.count - 1) + } + }() + + if #available(iOS 10, *) { + UNUserNotificationCenter.currentNotificationCenter().getNotificationSettingsWithCompletionHandler({ (settings) in + guard settings.alertSetting == .Enabled else {return} + let content = UNMutableNotificationContent() + content.title = title + content.body = body + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false) + let request = UNNotificationRequest(identifier: "org.kiwix.downloadFinished", content: content, trigger: trigger) + UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: nil) + }) + } else { + let notification = UILocalNotification() + notification.alertTitle = title + notification.alertBody = body + notification.soundName = UILocalNotificationDefaultSoundName + UIApplication.sharedApplication().presentLocalNotificationNow(notification) + } } } + // MARK: - NSURLSessionTaskDelegate func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { @@ -112,8 +142,9 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe // - Remove cache, if any // - Delete Download task Object - context.performBlock { + context.performBlockAndWait { guard let book = Book.fetch(bookID, context: self.context) else {return} + if let title = book.title {self.downloadedBookTitle.append(title)} book.removeResumeData() guard let downloadTask = book.downloadTask else {return} self.context.deleteObject(downloadTask)