Skip to content

Commit 9e8faf7

Browse files
committed
Local notification WORKS
This took two hours to debug, thanks Apple
1 parent 5de810f commit 9e8faf7

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

BrickHack-Mobile/AppDelegate.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import UIKit
1010

1111
@UIApplicationMain
12-
class AppDelegate: UIResponder, UIApplicationDelegate {
12+
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
1313

1414
var window: UIWindow?
1515

1616
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
17-
// Override point for customization after application launch.
17+
UNUserNotificationCenter.current().delegate = self
1818
return true
1919
}
2020

@@ -29,28 +29,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2929
return false
3030
}
3131

32-
func applicationWillResignActive(_ application: UIApplication) {
33-
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
34-
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
35-
}
32+
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
3633

37-
func applicationDidEnterBackground(_ application: UIApplication) {
38-
// 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.
39-
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
34+
print("WILL PRESENT \(notification)")
35+
completionHandler([.alert, .sound])
4036
}
4137

42-
func applicationWillEnterForeground(_ application: UIApplication) {
43-
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
44-
}
45-
46-
func applicationDidBecomeActive(_ application: UIApplication) {
47-
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
48-
}
49-
50-
func applicationWillTerminate(_ application: UIApplication) {
51-
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
52-
}
5338

5439

5540
}
56-

BrickHack-Mobile/Controllers/ScheduleTableViewController.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import UIKit
1010
import TimelineTableViewCell
1111
import SwiftMessages
12+
import UserNotifications
1213

13-
class ScheduleTableViewController: UITableViewController {
14+
class ScheduleTableViewController: UITableViewController, UNUserNotificationCenterDelegate {
1415

1516
// MARK: Ivars
1617
var scheduleTimer = Timer()
@@ -166,6 +167,8 @@ class ScheduleTableViewController: UITableViewController {
166167
}
167168

168169

170+
// MARK: Some helper functions
171+
169172
// Helper function to get a global index for an event from its local table index
170173
// Section > Row:
171174
// 0
@@ -233,19 +236,28 @@ class ScheduleTableViewController: UITableViewController {
233236
// @TODO: Handle updating favorite with server
234237
}
235238

239+
// MARK: Notifications
236240
private func scheduleFavoriteNotification(forEvent timelineEvent: TimelineEvent) {
237241

238242
askForNotificationPermissionIfNeeded()
239243

240-
// Extract components and set trigger
241-
let components = Calendar.current.dateComponents(in: .current, from: timelineEvent.event.time)
242-
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
244+
// If this looks complicated, see this StackOverflow answer:
245+
// https://stackoverflow.com/a/60134234/1431900
246+
let now = Date(timeIntervalSinceNow: 0)
247+
248+
// If event has already occured, silently fail.
249+
if (timelineEvent.event.time < now) {
250+
return
251+
}
243252

244-
print(timelineEvent.event.uuid)
253+
// Otherwise, let's calculate the time until the next event
254+
let interval = timelineEvent.event.time.timeIntervalSince(Date(timeIntervalSinceNow: 0))
255+
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
245256

246257
let content = UNMutableNotificationContent()
247258
content.title = timelineEvent.event.title + " is starting!"
248259
content.body = timelineEvent.event.description
260+
content.sound = .default
249261
let request = UNNotificationRequest(identifier: timelineEvent.event.uuid, content: content, trigger: trigger)
250262

251263
// Add request to local notification center
@@ -265,12 +277,18 @@ class ScheduleTableViewController: UITableViewController {
265277
}
266278

267279
private func askForNotificationPermissionIfNeeded() {
280+
268281
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
269-
if !granted {
270282

283+
print("NOTIF ERROR: \(error)")
284+
if !granted {
271285
DispatchQueue.main.async {
272286
MessageHandler.showNotificationDisabledInfo()
273287
}
288+
} else {
289+
DispatchQueue.main.async {
290+
UIApplication.shared.registerForRemoteNotifications()
291+
}
274292
}
275293
}
276294
}
@@ -343,8 +361,6 @@ class ScheduleTableViewController: UITableViewController {
343361

344362
// Otherwise, go on to configure this current section as "passed"
345363
self.timelineEvents.filter({ $0.event.section == sectionIndex }).forEach { timelineEvent in
346-
347-
print("updated \(timelineEvent)")
348364
timelineEvent.allColor = self.backColor
349365
}
350366

0 commit comments

Comments
 (0)