forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSCUIApplicationDelegate.swift
More file actions
157 lines (121 loc) · 5.61 KB
/
NSCUIApplicationDelegate.swift
File metadata and controls
157 lines (121 loc) · 5.61 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Foundation
import UIKit
import GoogleUtilities
#if canImport(FirebaseMessaging)
import FirebaseMessaging
#endif
#if canImport(FirebaseAuth)
import FirebaseAuth
#endif
#if canImport(FirebaseAuthUI)
import FirebaseAuthUI
#endif
import NSCFirebaseMessagingCore
@objc(NSCUIApplicationDelegate)
public class NSCUIApplicationDelegate: UIResponder , UIApplicationDelegate {
public static let sharedInstance = NSCUIApplicationDelegate()
public func observe(){
GULAppDelegateSwizzler.registerAppDelegateInterceptor(NSCUIApplicationDelegate.sharedInstance)
GULAppDelegateSwizzler.proxyOriginalDelegateIncludingAPNSMethods()
guard GULAppDelegateSwizzler.sharedApplication()?.delegate != nil else {
return
}
let selector = #selector((NSCUIApplicationDelegate).application(_:didReceiveRemoteNotification:fetchCompletionHandler:))
if (!(GULAppDelegateSwizzler.sharedApplication()!.delegate!.responds(to: selector))) {
let method = class_getInstanceMethod(
object_getClass(NSCUIApplicationDelegate.sharedInstance),
selector
);
guard method != nil else {
return
}
class_addMethod(
object_getClass(
GULAppDelegateSwizzler.sharedApplication()!.delegate
), selector,
method_getImplementation(method!),
method_getTypeEncoding(method!)
);
}
let tokenSelector = #selector(NSCUIApplicationDelegate.application(_:didRegisterForRemoteNotificationsWithDeviceToken:))
if (!(GULAppDelegateSwizzler.sharedApplication()!.delegate!.responds(to: tokenSelector))) {
let method = class_getInstanceMethod(
object_getClass(NSCUIApplicationDelegate.sharedInstance),
tokenSelector
);
guard method != nil else {
return
}
class_addMethod(
object_getClass(
GULAppDelegateSwizzler.sharedApplication()!.delegate
), tokenSelector,
method_getImplementation(method!),
method_getTypeEncoding(method!)
);
}
let urlHandlingSelector = #selector(NSCUIApplicationDelegate.application(_:open:options:))
if (!(GULAppDelegateSwizzler.sharedApplication()!.delegate!.responds(to: urlHandlingSelector))) {
let method = class_getInstanceMethod(
object_getClass(NSCUIApplicationDelegate.sharedInstance),
urlHandlingSelector
);
guard method != nil else {
return
}
class_addMethod(
object_getClass(
GULAppDelegateSwizzler.sharedApplication()!.delegate
), urlHandlingSelector,
method_getImplementation(method!),
method_getTypeEncoding(method!)
);
}
}
@objc public func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
#if canImport(FirebaseAuth)
if(Auth.auth().canHandle(url)){
return true
}
#endif
#if canImport(FirebaseAuthUI)
let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String?
if(FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false){
return true
}
#endif
return false
}
@objc public func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback?(false,error as NSError)
NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback = nil
}
@objc public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
#if canImport(FirebaseMessaging)
Messaging.messaging().apnsToken = deviceToken
#endif
UserDefaults.standard.set(true, forKey: NSCNotificationHelper.REMOTE_NOTIFICATIONS_REGISTRATION_STATUS)
NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback?(UIApplication.shared.isRegisteredForRemoteNotifications, nil)
//NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback = nil
#if !canImport(FirebaseMessaging)
NSCFirebaseMessagingCore.onTokenCallback?(NSCFirebaseMessagingCore.apnsToken(toString: deviceToken))
// NSCFirebaseMessagingCore.onTokenCallback = nil
#endif
}
@objc public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
#if canImport(FirebaseAuth)
if(Auth.auth().canHandleNotification(userInfo)){
completionHandler(.noData)
return
}
#endif
var message = userInfo
#if canImport(FirebaseMessaging)
message = parseRemoteMessage(userInfo)
#endif
message["foreground"] = application.applicationState == UIApplication.State.active
NSCFirebaseMessagingCore.onMessageCallback?(message) {
completionHandler(.newData)
}
}
}