forked from RocketChat/Rocket.Chat.iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.swift
90 lines (72 loc) · 3.01 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// AppDelegate.swift
// Rocket.Chat
//
// Created by Rafael K. Streit on 7/5/16.
// Copyright © 2016 Rocket.Chat. All rights reserved.
//
import UIKit
import RealmSwift
import UserNotifications
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var notificationWindow: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Launcher().prepareToLaunch(with: launchOptions)
PushManager.setupNotificationCenter()
application.registerForRemoteNotifications()
if let launchOptions = launchOptions,
let notification = launchOptions[.remoteNotification] as? [AnyHashable: Any] {
PushManager.handleNotification(raw: notification)
}
// If user is authenticated, open the chat right away
// but if not, just open the authentication screen.
if let auth = AuthManager.isAuthenticated() {
AuthManager.persistAuthInformation(auth)
AuthSettingsManager.shared.updateCachedSettings()
WindowManager.open(.chat)
} else {
WindowManager.open(.auth(serverUrl: "", credentials: nil))
}
initNotificationWindow()
return true
}
func initNotificationWindow() {
notificationWindow = TransparentToTouchesWindow(frame: UIScreen.main.bounds)
notificationWindow?.rootViewController = NotificationViewController.shared
notificationWindow?.windowLevel = UIWindowLevelAlert
notificationWindow?.makeKeyAndVisible()
}
// MARK: AppDelegate LifeCycle
func applicationDidBecomeActive(_ application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
}
func applicationDidEnterBackground(_ application: UIApplication) {
SubscriptionManager.updateUnreadApplicationBadge()
if AuthManager.isAuthenticated() != nil {
UserManager.setUserPresence(status: .away) { (_) in
SocketManager.disconnect({ (_, _) in })
}
}
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
if AppManager.handleDeepLink(url) != nil {
return true
}
return GIDSignIn.sharedInstance().handle(
url,
sourceApplication: options[.sourceApplication] as? String,
annotation: options[.annotation]
)
}
// MARK: Remote Notification
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
UserDefaults.group.set(deviceToken.hexString, forKey: PushManager.kDeviceTokenKey)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Log.debug("Fail to register for notification: \(error)")
}
}