-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDelegate.swift
76 lines (65 loc) · 3.26 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI
import Firebase
import FirebaseMessaging
import Resolver
@objc
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
if ProcessInfo.processInfo.environment["boot_type"] == "clean" {
Log.info("Running in clean boot mode, erasing all data and logging out before the app launches")
performCleanBoot()
}
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if let visabilityValue = notification.request.content.userInfo[NotificationInfoKeys.visability.rawValue] as? String,
let visability = NotificationVisability(rawValue: visabilityValue) {
switch visability {
case .prominent:
completionHandler([.banner, .list, .sound])
case .visible:
completionHandler([.banner, .list])
case .unnoticed:
completionHandler([.list])
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
Log.info("Got APNS token: \(tokenString)")
Messaging.messaging().apnsToken = deviceToken
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Log.error("Couldnt register for APNS: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Log.info("Got APNS message: \(userInfo)")
let handler = Resolver.resolve(RemoteNotificationsHandler.self)
handler.handleSystemNotification(userInfo: userInfo, fetchCompletionHandler: completionHandler)
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
Log.info("fcm token received: \(fcmToken ?? "none")")
Messaging.messaging().subscribe(toTopic: "feature_flags")
}
private func performCleanBoot() {
let group = DispatchGroup()
group.enter()
Resolver.resolve(DataEraser.self).eraseAllData(completion: { _ in
group.leave()
})
group.wait()
do {
try Resolver.resolve(Deauthorizable.self).deauthorize()
} catch {
Log.warning("Couldn't deauthorize user: \(error.localizedDescription)")
}
}
}