-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate+Firebase.swift
96 lines (72 loc) · 3.13 KB
/
AppDelegate+Firebase.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
91
92
93
94
95
96
//
// AppDelegate+Firebase.swift
//
//
// Created by Viktor Olesenko on 03.03.17.
// Copyright © 2017 Steelkiwi. All rights reserved.
//
import UserNotifications
import Firebase
extension AppDelegate {
typealias PushNotification = [AnyHashable : Any]
internal func initNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
guard granted else { return }
self.getNotificationSettings()
}
UNUserNotificationCenter.current().delegate = self
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configureIfNeeded()
Messaging.messaging().delegate = self
}
private func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
// If not simulator
if error.code != 3010 {
print("APNS ERROR: " + error.localizedDescription)
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
openNotification(userData: userInfo)
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
// TODO: Store token
}
}
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
openNotification(userData: userInfo)
let displayOptions: UNNotificationPresentationOptions = [] // badge, alert, sound
completionHandler(displayOptions)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
openNotification(userData: userInfo)
completionHandler()
}
}
extension FirebaseApp {
static var isConfigured: Bool {
return FirebaseApp.app() != nil
}
static func configureIfNeeded() {
guard isConfigured == false else { return }
configure()
}
}