Download Notification

This commit is contained in:
Chris Li 2016-09-20 11:25:56 -04:00
parent 555125c01e
commit cd9c4d4c58
6 changed files with 50 additions and 38 deletions

View File

@ -8,6 +8,7 @@
import UIKit
import CoreData
import Operations
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
@ -24,9 +25,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
func registerNotification() {
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) {

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.873</string>
<string>1.8.900</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -8,6 +8,8 @@
import UIKit
class DownloadFinishedNotifications: {
class DownloadFinishedNotifications: UILocalNotification {
}
//@available(iOS 10, *)

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.876</string>
<string>1.8.903</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -19,21 +19,5 @@
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Kiwix-iOS/AppDelegate.swift"
timestampString = "496013317.729722"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "149"
endingLineNumber = "149"
landmarkName = "applicationDidEnterBackground(application:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -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 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 = NSLocalizedString("Book download finished", comment: "Notification: Book download finished")
notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished")
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)