| 1 |
efrain |
1 |
//
|
|
|
2 |
// AppDelegate.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 2/23/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Foundation
|
|
|
9 |
import Firebase
|
|
|
10 |
import Messages
|
|
|
11 |
import BackgroundTasks
|
|
|
12 |
|
|
|
13 |
//UIResponder, UIApplicationDelegate {
|
|
|
14 |
|
| 59 |
efrain |
15 |
class SyncForegroundObservableObject: ObservableObject
|
|
|
16 |
{
|
|
|
17 |
@Published var timer : Timer = Timer()
|
|
|
18 |
}
|
|
|
19 |
|
| 1 |
efrain |
20 |
class AppDelegate : NSObject, UIApplicationDelegate {
|
|
|
21 |
private var isSyncInProgress = false
|
|
|
22 |
private var syncAdapter = SyncAdapter()
|
|
|
23 |
private let gcmMessageIDKey = "gcm.message_id"
|
| 17 |
efrain |
24 |
private var appData = AppData.sharedInstance
|
| 1 |
efrain |
25 |
|
| 17 |
efrain |
26 |
|
| 1 |
efrain |
27 |
static var orientationLock = UIInterfaceOrientationMask.portrait
|
|
|
28 |
|
|
|
29 |
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
|
|
|
30 |
return AppDelegate.orientationLock
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
|
|
|
34 |
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
|
|
35 |
|
| 11 |
efrain |
36 |
let database = Database.sharedInstance
|
|
|
37 |
if database.open() != nil {
|
|
|
38 |
database.createTables()
|
|
|
39 |
}
|
| 1 |
efrain |
40 |
|
| 17 |
efrain |
41 |
if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
|
|
|
42 |
|
|
|
43 |
let old_uuid = appData.deviceUuid
|
|
|
44 |
if new_uuid != old_uuid {
|
|
|
45 |
var sync = SyncModel()
|
|
|
46 |
sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
|
|
|
47 |
sync.data = new_uuid
|
|
|
48 |
|
|
|
49 |
if SyncDao.sharedInstance.insert(record: sync) > 0 {
|
|
|
50 |
appData.deviceUuid = new_uuid
|
|
|
51 |
appData.save()
|
|
|
52 |
|
|
|
53 |
syncAdapter.sync {
|
|
|
54 |
success in
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
}
|
| 1 |
efrain |
58 |
|
|
|
59 |
}
|
|
|
60 |
|
| 17 |
efrain |
61 |
|
| 1 |
efrain |
62 |
FirebaseApp.configure()
|
|
|
63 |
|
| 17 |
efrain |
64 |
Messaging.messaging().delegate = self
|
|
|
65 |
|
|
|
66 |
if #available(iOS 10.0, *) {
|
|
|
67 |
// For iOS 10 display notification (sent via APNS)
|
|
|
68 |
UNUserNotificationCenter.current().delegate = self
|
|
|
69 |
|
|
|
70 |
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
|
|
|
71 |
UNUserNotificationCenter.current().requestAuthorization(
|
|
|
72 |
options: authOptions,
|
|
|
73 |
completionHandler: {_, _ in })
|
|
|
74 |
} else {
|
|
|
75 |
let settings: UIUserNotificationSettings =
|
|
|
76 |
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
|
|
|
77 |
application.registerUserNotificationSettings(settings)
|
|
|
78 |
}
|
|
|
79 |
|
| 59 |
efrain |
80 |
application.registerForRemoteNotifications()
|
| 58 |
efrain |
81 |
|
|
|
82 |
registerBGTasksScheduler()
|
|
|
83 |
scheduleProcess()
|
|
|
84 |
scheduleRefresh()
|
|
|
85 |
|
| 59 |
efrain |
86 |
return true
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
func registerBGTasksScheduler() {
|
|
|
90 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_REFRESH, using: nil) { task in
|
|
|
91 |
self.handleRefreshTask(task: task as! BGAppRefreshTask)
|
|
|
92 |
}
|
| 58 |
efrain |
93 |
|
| 59 |
efrain |
94 |
BGTaskScheduler.shared.register(forTaskWithIdentifier: Constants.BACKGROUND_TASK_PROCESS, using: nil) { task in
|
|
|
95 |
self.handleProcessTask(task: task as! BGProcessingTask)
|
|
|
96 |
}
|
| 17 |
efrain |
97 |
|
| 59 |
efrain |
98 |
}
|
|
|
99 |
|
|
|
100 |
func scheduleRefresh() {
|
|
|
101 |
let request = BGAppRefreshTaskRequest(identifier: Constants.BACKGROUND_TASK_REFRESH)
|
|
|
102 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
103 |
|
| 1 |
efrain |
104 |
|
| 59 |
efrain |
105 |
|
|
|
106 |
do {
|
|
|
107 |
try BGTaskScheduler.shared.submit(request)
|
|
|
108 |
} catch {
|
|
|
109 |
print("Could not schedule app refresh: \(error)")
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
func scheduleProcess() {
|
|
|
114 |
let request = BGProcessingTaskRequest(identifier: Constants.BACKGROUND_TASK_PROCESS)
|
|
|
115 |
request.earliestBeginDate = Date(timeIntervalSinceNow: 10 * 60)
|
|
|
116 |
request.requiresNetworkConnectivity = true
|
|
|
117 |
request.requiresExternalPower = false
|
| 1 |
efrain |
118 |
|
| 59 |
efrain |
119 |
do {
|
|
|
120 |
try BGTaskScheduler.shared.submit(request)
|
|
|
121 |
} catch {
|
|
|
122 |
print("Could not schedule processing: \(error)")
|
|
|
123 |
}
|
| 1 |
efrain |
124 |
}
|
| 59 |
efrain |
125 |
|
|
|
126 |
func handleProcessTask(task: BGProcessingTask) {
|
|
|
127 |
scheduleProcess()
|
|
|
128 |
|
|
|
129 |
let syncAdapter = SyncAdapter()
|
|
|
130 |
syncAdapter.sync { success in
|
|
|
131 |
task.setTaskCompleted(success: success)
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/*
|
|
|
135 |
task.expirationHandler = {
|
|
|
136 |
task.setTaskCompleted(success: false)
|
|
|
137 |
}
|
|
|
138 |
*/
|
|
|
139 |
task.setTaskCompleted(success: true)
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
func handleRefreshTask(task: BGAppRefreshTask) {
|
|
|
143 |
scheduleRefresh()
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
let syncAdapter = SyncAdapter()
|
|
|
148 |
if syncAdapter.isCheckChangesRequired() {
|
|
|
149 |
syncAdapter.checkChanges(isForeground: false) { success in
|
|
|
150 |
task.setTaskCompleted(success: success)
|
|
|
151 |
}
|
|
|
152 |
} else {
|
|
|
153 |
task.expirationHandler = {
|
|
|
154 |
task.setTaskCompleted(success: false)
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
task.setTaskCompleted(success: true)
|
|
|
160 |
}
|
| 1 |
efrain |
161 |
|
| 58 |
efrain |
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
| 17 |
efrain |
166 |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
|
|
167 |
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
|
|
168 |
|
|
|
169 |
if let messageID = userInfo[gcmMessageIDKey] {
|
|
|
170 |
print("Message ID: \(messageID)")
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
processNotification(userInfo: userInfo, isForeground: false)
|
|
|
175 |
completionHandler(UIBackgroundFetchResult.newData)
|
|
|
176 |
}
|
| 1 |
efrain |
177 |
|
| 17 |
efrain |
178 |
|
| 8 |
efrain |
179 |
|
| 17 |
efrain |
180 |
}
|
| 1 |
efrain |
181 |
|
| 17 |
efrain |
182 |
extension AppDelegate: MessagingDelegate {
|
|
|
183 |
|
|
|
184 |
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
|
| 1 |
efrain |
185 |
|
| 17 |
efrain |
186 |
let token = fcmToken ?? ""
|
|
|
187 |
print("token: ", token)
|
| 1 |
efrain |
188 |
|
| 17 |
efrain |
189 |
|
|
|
190 |
if appData.fcmToken != token {
|
|
|
191 |
|
|
|
192 |
var sync = SyncModel()
|
|
|
193 |
sync.data = token
|
|
|
194 |
sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
|
|
|
195 |
|
|
|
196 |
if SyncDao.sharedInstance.insert(record: sync) > 0 {
|
|
|
197 |
|
|
|
198 |
appData.fcmToken = token
|
|
|
199 |
appData.save()
|
|
|
200 |
|
|
|
201 |
syncAdapter.sync {
|
|
|
202 |
success in
|
| 1 |
efrain |
203 |
}
|
|
|
204 |
}
|
|
|
205 |
}
|
| 17 |
efrain |
206 |
}
|
| 1 |
efrain |
207 |
}
|
|
|
208 |
|
|
|
209 |
@available(iOS 10, *)
|
| 17 |
efrain |
210 |
extension AppDelegate : UNUserNotificationCenterDelegate {
|
|
|
211 |
|
| 1 |
efrain |
212 |
// Receive displayed notifications for iOS 10 devices.
|
|
|
213 |
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
|
214 |
willPresent notification: UNNotification,
|
| 17 |
efrain |
215 |
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
|
216 |
let userInfo = notification.request.content.userInfo
|
| 1 |
efrain |
217 |
|
| 17 |
efrain |
218 |
if let messageID = userInfo[gcmMessageIDKey] {
|
|
|
219 |
print("Message ID: \(messageID)")
|
|
|
220 |
}
|
| 1 |
efrain |
221 |
|
| 17 |
efrain |
222 |
processNotification(userInfo: userInfo, isForeground: true)
|
|
|
223 |
completionHandler([[.banner, .badge, .sound]])
|
|
|
224 |
}
|
| 8 |
efrain |
225 |
|
| 17 |
efrain |
226 |
private func processNotification(userInfo : [AnyHashable : Any], isForeground : Bool) {
|
| 22 |
efrain |
227 |
let now = Date()
|
|
|
228 |
let dateFormatter = DateFormatter()
|
| 23 |
efrain |
229 |
dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
|
|
|
230 |
|
|
|
231 |
let timeFormatter = DateFormatter()
|
|
|
232 |
timeFormatter.dateFormat = Constants.FORMAT_TIME_12
|
| 45 |
efrain |
233 |
/*
|
| 22 |
efrain |
234 |
let dateOn = dateFormatter.string(from: now)
|
| 23 |
efrain |
235 |
let timeOn = dateFormatter.string(from: now)
|
|
|
236 |
|
|
|
237 |
let userNotificationDao = UserNotificationDao.sharedInstance
|
|
|
238 |
var userNotification : UserNotificationModel
|
|
|
239 |
|
| 45 |
efrain |
240 |
|
| 24 |
efrain |
241 |
if Config.DEBUG && !appData.userUuid.isEmpty {
|
| 23 |
efrain |
242 |
userNotification = UserNotificationModel(userUuid: appData.userUuid, title: "userInfo", body: "\(userInfo)", viewed: 0, url: "", dateOn: dateOn, timeOn: timeOn)
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
userNotificationDao.insert(userNotification: userNotification)
|
|
|
246 |
}
|
| 24 |
efrain |
247 |
*/
|
| 23 |
efrain |
248 |
|
| 24 |
efrain |
249 |
guard let apsDict = userInfo["aps"]as?[String:Any] else {return}
|
|
|
250 |
guard let alertDict = apsDict["alert"]as?[String:Any]else {return}
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
let title = alertDict["title"] as? String ?? ""
|
|
|
254 |
let body = alertDict["body"] as? String ?? ""
|
| 44 |
efrain |
255 |
let url = userInfo.index(forKey: "url") == nil ? "" : userInfo["url"] as? String ?? ""
|
|
|
256 |
let command = userInfo.index(forKey: "command") == nil ? "" : userInfo["command"] as? String ?? ""
|
| 43 |
efrain |
257 |
let newCapsules = userInfo.index(forKey: "new_capsules") == nil ? 0 : Int(userInfo["new_capsules"] as? String ?? "") ?? 0
|
| 44 |
efrain |
258 |
|
|
|
259 |
|
| 17 |
efrain |
260 |
if command == "signout" {
|
| 39 |
efrain |
261 |
NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_EXIT , object: self, userInfo: nil)
|
| 32 |
efrain |
262 |
}
|
| 44 |
efrain |
263 |
else if command == "content-refresh" {
|
| 32 |
efrain |
264 |
let userinfo = [
|
| 27 |
efrain |
265 |
"title" : title,
|
|
|
266 |
"body" : body,
|
|
|
267 |
"new_capsules" : String(newCapsules),
|
|
|
268 |
"is_foreground" : (isForeground ? "1" : "0")
|
|
|
269 |
]
|
| 17 |
efrain |
270 |
|
|
|
271 |
NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_COMMAND_REFRESH_CONTENT , object: self, userInfo: userinfo)
|
| 27 |
efrain |
272 |
} else {
|
| 8 |
efrain |
273 |
|
| 27 |
efrain |
274 |
let userinfo = [
|
|
|
275 |
"title" : title,
|
|
|
276 |
"body" : body,
|
|
|
277 |
"url" : url,
|
|
|
278 |
]
|
| 8 |
efrain |
279 |
|
| 31 |
efrain |
280 |
NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_PUSH , object: self, userInfo: userInfo)
|
| 25 |
efrain |
281 |
|
| 23 |
efrain |
282 |
|
| 17 |
efrain |
283 |
}
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
|
|
287 |
|
|
|
288 |
print( "didRegisterForRemoteNotificationsWithDeviceToken")
|
| 8 |
efrain |
289 |
|
| 17 |
efrain |
290 |
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
|
|
|
291 |
let token = tokenParts.joined()
|
|
|
292 |
print("token: ", token)
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
if appData.fcmToken != token {
|
|
|
296 |
|
|
|
297 |
var sync = SyncModel()
|
|
|
298 |
sync.data = token
|
|
|
299 |
sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
|
|
|
300 |
|
|
|
301 |
if SyncDao.sharedInstance.insert(record: sync) > 0 {
|
|
|
302 |
|
|
|
303 |
appData.fcmToken = token
|
|
|
304 |
appData.save()
|
|
|
305 |
|
|
|
306 |
syncAdapter.sync {
|
|
|
307 |
success in
|
|
|
308 |
}
|
|
|
309 |
}
|
| 8 |
efrain |
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
| 17 |
efrain |
315 |
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
|
|
316 |
print( "didFailToRegisterForRemoteNotificationsWithError")
|
|
|
317 |
}
|
| 8 |
efrain |
318 |
|
| 1 |
efrain |
319 |
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
|
320 |
didReceive response: UNNotificationResponse,
|
|
|
321 |
withCompletionHandler completionHandler: @escaping () -> Void) {
|
| 17 |
efrain |
322 |
let userInfo = response.notification.request.content.userInfo
|
|
|
323 |
|
|
|
324 |
if let messageID = userInfo[gcmMessageIDKey] {
|
|
|
325 |
print("Message ID from userNotificationCenter didReceive: \(messageID)")
|
|
|
326 |
}
|
| 1 |
efrain |
327 |
|
|
|
328 |
|
| 17 |
efrain |
329 |
processNotification(userInfo: userInfo, isForeground: true)
|
| 1 |
efrain |
330 |
|
|
|
331 |
completionHandler()
|
|
|
332 |
}
|
|
|
333 |
}
|