-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
140 lines (118 loc) · 4.85 KB
/
Copy pathAppDelegate.swift
File metadata and controls
140 lines (118 loc) · 4.85 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
//
// AppDelegate.swift
// ReactNativeSdkExample
//
// Created by Loren Posen on 6/11/25.
//
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import UserNotifications
import IterableSDK
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var reactNativeDelegate: ReactNativeDelegate?
var reactNativeFactory: RCTReactNativeFactory?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
NSLog("FINISHED LAUNCHING WITH OPTIONS")
ITBInfo()
let delegate = ReactNativeDelegate()
let factory = RCTReactNativeFactory(delegate: delegate)
delegate.dependencyProvider = RCTAppDependencyProvider()
reactNativeDelegate = delegate
reactNativeFactory = factory
window = UIWindow(frame: UIScreen.main.bounds)
factory.startReactNative(
withModuleName: "ReactNativeSdkExample",
in: window,
launchOptions: launchOptions
)
setupUserNotificationCenter()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NSLog("REGISTERED FOR REMOTE NOTIFICATIONS")
ITBInfo()
IterableAPI.register(token: deviceToken)
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError
error: Error) {
NSLog("FAILED TO REGISTER FOR REMOTE NOTIFICATIONS")
ITBInfo("error: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NSLog("RECEIVED REMOTE NOTIFICATIONS")
ITBInfo()
IterableAppIntegration.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
NSLog("RECEIVED UNIVERSAL LINK")
NSLog("userActivity: \(userActivity)")
ITBInfo()
guard let url = userActivity.webpageURL else {
return false
}
return IterableAPI.handle(universalLink: url)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
NSLog("OPEN URL")
NSLog("url: \(url)")
NSLog("options: \(options)")
ITBInfo()
return RCTLinkingManager.application(app, open: url, options: options)
}
private func setupUserNotificationCenter() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { settings in
if settings.authorizationStatus != .authorized {
ITBInfo("Not authorized")
// not authorized, ask for permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, _ in
ITBInfo("auth: \(success)")
if success {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
// TODO: Handle error etc.
}
} else {
// already authorized
ITBInfo("Already authorized")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
}
class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
override func sourceURL(for bridge: RCTBridge) -> URL? {
self.bundleURL()
}
override func bundleURL() -> URL? {
#if DEBUG
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
#else
Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
// App is running in the foreground
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("WILL PRESENT NOTIFICATION")
completionHandler([.alert, .badge, .sound])
}
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
NSLog("DID RECEIVE NOTIFICATION RESPONSE")
IterableAppIntegration.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
}