Polish up notification subscription functionality

This commit is contained in:
CypherPoet 2019-02-25 23:42:40 -05:00
parent 812e197db6
commit e455428abd
2 changed files with 36 additions and 6 deletions

View File

@ -7,15 +7,28 @@
//
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let notificationAuthorizationOptions: UNAuthorizationOptions = [.alert, .sound, .badge]
let notificationPresentationOptions: UNNotificationPresentationOptions = [.alert, .sound, .badge]
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options: notificationAuthorizationOptions) {
(granted: Bool, error: Error?) in
if let error = error {
print("Error while attempting to grant notification authorization:\n\(error.localizedDescription)")
} else {
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
}
}
return true
}
@ -40,7 +53,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
/**
Show the notification, even when the app is currently running
*/
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler(notificationPresentationOptions)
}
}

View File

@ -131,7 +131,7 @@ class MyGenresTableViewController: UITableViewController {
}
func makeSubscriptions() {
subscribeLoop: for genre in userGenres {
for genre in userGenres {
let notificationInfo = CKSubscription.NotificationInfo()
notificationInfo.alertBody = "There's a new sound bite for the \"\(genre)\" genre"
@ -151,8 +151,12 @@ class MyGenresTableViewController: UITableViewController {
self.displayBasicAlert(
title: "Save error",
message: """
An error occured while attempting to save your subscription\
to the "\(genre)" genre :\n\(error.localizedDescription)\nPlease try again later.
An error occured while attempting to save your subscription \
to the "\(genre)" genre:
\
\(error.localizedDescription)
\
Please try again later.
"""
)
}