forked from customerio/customerio-reactnative
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
151 lines (114 loc) · 4.38 KB
/
AppDelegate.swift
File metadata and controls
151 lines (114 loc) · 4.38 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
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import UserNotifications
#if USE_FCM
import FirebaseMessaging
import FirebaseCore
import CioMessagingPushFCM
import CioFirebaseWrapper
typealias CioMessagingPushHandler = MessagingPushFCM
let UNIVERSAL_LINK_URL = URL(string: "http://www.amiapp-reactnative-fcm.com")!
#else
import CioMessagingPushAPN
typealias CioMessagingPushHandler = MessagingPushAPN
let UNIVERSAL_LINK_URL = URL(string: "http://www.amiapp-reactnative-apns.com")!
#endif
@main
class AppDelegateWithCioIntegration: CioAppDelegateWrapper<AppDelegate> {}
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var reactNativeDelegate: ReactNativeDelegate?
var reactNativeFactory: RCTReactNativeFactory?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
let delegate = ReactNativeDelegate()
let factory = RCTReactNativeFactory(delegate: delegate)
delegate.dependencyProvider = RCTAppDependencyProvider()
reactNativeDelegate = delegate
reactNativeFactory = factory
window = UIWindow(frame: UIScreen.main.bounds)
let remotePush = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: [String: [String: String]]]
if let link = remotePush?["CIO"]?["push"]?["link"], let url = URL(string:link) {
var launchOptions = launchOptions ?? [:]
if launchOptions[UIApplication.LaunchOptionsKey.url] == nil {
launchOptions[UIApplication.LaunchOptionsKey.url] = url
}
}
let appName = Bundle.main.displayName
factory.startReactNative(
withModuleName: appName,
in: window,
initialProperties: ["appName": appName],
launchOptions: launchOptions
)
#if USE_FCM
FirebaseApp.configure()
Messaging.messaging().delegate = self
#endif
CioMessagingPushHandler.initialize(withConfig: MessagingPushConfigBuilder().build())
return true
}
}
// MARK: Deep linking
extension AppDelegate {
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return RCTLinkingManager.application(app, open: url, options: options)
}
func application(
_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let url = userActivity.webpageURL, (url.scheme == "http" || url.scheme == "https") && url.host() == UNIVERSAL_LINK_URL.host() {
return RCTLinkingManager.application(
application,
continue: userActivity,
restorationHandler: restorationHandler
)
}
return false
}
}
// MARK: Push setup
#if USE_FCM
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// Not needed when CioAppDelegateWrapper is used
// MessagingPush.shared.messaging(messaging, didReceiveRegistrationToken: fcmToken)
}
}
#else
extension AppDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Not needed when CioAppDelegateWrapper is used
// MessagingPush.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: any Error) {
// Not needed when CioAppDelegateWrapper is used
// MessagingPush.shared.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}
}
#endif
// MARK: React Native Setup
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
}
}
// MARK: React Native Sample App Utils
extension Bundle {
var displayName: String {
let name = object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
return name ?? object(forInfoDictionaryKey: kCFBundleNameKey as String) as! String
}
}