| 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 |
|
|
|
15 |
class AppDelegate : NSObject, UIApplicationDelegate {
|
|
|
16 |
private var isSyncInProgress = false
|
|
|
17 |
private var syncAdapter = SyncAdapter()
|
|
|
18 |
private let gcmMessageIDKey = "gcm.message_id"
|
| 9 |
efrain |
19 |
private let appDao = AppDao.sharedInstance
|
| 1 |
efrain |
20 |
|
|
|
21 |
static var orientationLock = UIInterfaceOrientationMask.portrait
|
|
|
22 |
|
|
|
23 |
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
|
|
|
24 |
return AppDelegate.orientationLock
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
|
|
|
28 |
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
|
|
29 |
|
| 11 |
efrain |
30 |
let database = Database.sharedInstance
|
|
|
31 |
if database.open() != nil {
|
|
|
32 |
database.createTables()
|
|
|
33 |
}
|
| 1 |
efrain |
34 |
|
| 11 |
efrain |
35 |
let appDao = AppDao.sharedInstance
|
|
|
36 |
var appData = appDao.selectOne()
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
if appData.id == 0 {
|
|
|
40 |
appData.deviceUuid = "-"
|
|
|
41 |
appDao.insert(model: appData)
|
|
|
42 |
}
|
|
|
43 |
|
| 1 |
efrain |
44 |
|
|
|
45 |
|
|
|
46 |
print("registro del dispositivo")
|
|
|
47 |
NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE, object: nil)
|
|
|
48 |
|
|
|
49 |
if let uuid = UIDevice.current.identifierForVendor?.uuidString {
|
|
|
50 |
let userInfo = ["uuid": uuid]
|
|
|
51 |
NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE , object: self, userInfo: userInfo)
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
NotificationCenter.default.addObserver(self, selector: #selector(receivingNewToken(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN, object: nil)
|
|
|
57 |
|
|
|
58 |
// Remote Notification Enable// Use Firebase library to configure APIs
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
print("Firebase configuracion")
|
|
|
62 |
FirebaseApp.configure()
|
|
|
63 |
|
|
|
64 |
UNUserNotificationCenter.current().delegate = self
|
|
|
65 |
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
|
|
|
66 |
UNUserNotificationCenter.current().requestAuthorization(
|
|
|
67 |
options: authOptions,
|
|
|
68 |
completionHandler: {_, _ in }
|
|
|
69 |
)
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
Messaging.messaging().delegate = self
|
|
|
75 |
|
|
|
76 |
print("Register For Remote Notifications")
|
|
|
77 |
application.registerForRemoteNotifications()
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
return true;
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
| 8 |
efrain |
88 |
|
| 1 |
efrain |
89 |
// Storage of the UID once the notification is received
|
|
|
90 |
@objc func receivingNewDevice(n: NSNotification){
|
|
|
91 |
|
|
|
92 |
print("receivingNewDevice")
|
|
|
93 |
if n.userInfo != nil{
|
|
|
94 |
if let new_uuid = n.userInfo?["uuid"]! as? String {
|
| 11 |
efrain |
95 |
var appData = appDao.selectOne()
|
| 1 |
efrain |
96 |
let old_uuid = appData.deviceUuid
|
|
|
97 |
if new_uuid != old_uuid {
|
|
|
98 |
var sync = SyncModel()
|
|
|
99 |
sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
|
|
|
100 |
sync.data = new_uuid
|
|
|
101 |
|
|
|
102 |
if SyncDao.sharedInstance.insert(record: sync) > 0 {
|
|
|
103 |
appData.deviceUuid = new_uuid
|
|
|
104 |
|
| 11 |
efrain |
105 |
print("update query : 18")
|
|
|
106 |
appDao.update(model: appData)
|
|
|
107 |
|
| 1 |
efrain |
108 |
|
|
|
109 |
|
|
|
110 |
syncAdapter.sync {
|
|
|
111 |
success in
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
@objc func receivingNewToken(n: NSNotification){
|
|
|
120 |
if n.userInfo != nil{
|
|
|
121 |
if let token = n.userInfo?["token"]! as? String {
|
|
|
122 |
if !token.isEmpty {
|
| 11 |
efrain |
123 |
var appData = appDao.selectOne()
|
| 1 |
efrain |
124 |
if appData.fcmToken != token {
|
|
|
125 |
|
|
|
126 |
var sync = SyncModel()
|
|
|
127 |
sync.data = token
|
|
|
128 |
sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
|
|
|
129 |
|
|
|
130 |
if SyncDao.sharedInstance.insert(record: sync) > 0 {
|
|
|
131 |
|
|
|
132 |
appData.fcmToken = token
|
|
|
133 |
|
| 11 |
efrain |
134 |
print("update query : 19")
|
|
|
135 |
appDao.update(model: appData)
|
|
|
136 |
|
| 1 |
efrain |
137 |
syncAdapter.sync {
|
|
|
138 |
success in
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
//Recibir mensajes
|
|
|
149 |
@available(iOS 10, *)
|
|
|
150 |
extension AppDelegate: UNUserNotificationCenterDelegate {
|
|
|
151 |
// Receive displayed notifications for iOS 10 devices.
|
|
|
152 |
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
|
153 |
willPresent notification: UNNotification,
|
|
|
154 |
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
|
|
|
155 |
-> Void) {
|
|
|
156 |
|
|
|
157 |
print("message userNotificationCenter iOS 10")
|
|
|
158 |
|
|
|
159 |
let userInfo = notification.request.content.userInfo
|
|
|
160 |
|
|
|
161 |
// With swizzling disabled you must let Messaging know about the message, for Analytics
|
|
|
162 |
// Messaging.messaging().appDidReceiveMessage(userInfo)
|
|
|
163 |
|
|
|
164 |
// ...
|
|
|
165 |
print(userInfo)
|
| 8 |
efrain |
166 |
|
|
|
167 |
if let aps = userInfo["aps"] as? [String:Any] {
|
|
|
168 |
//let badgeNumber = aps["badge"] as! Int
|
|
|
169 |
//application.applicationIconBadgeNumber = badgeNumber
|
|
|
170 |
|
|
|
171 |
//print("aps: \(aps)")
|
| 1 |
efrain |
172 |
|
| 8 |
efrain |
173 |
|
|
|
174 |
//if let alert = aps["alert"]as? [String:Any] {
|
|
|
175 |
|
|
|
176 |
// let title = alert["title"] ? alert["title"]! : ""
|
|
|
177 |
// let body = alert["body"] ? alert["body"]! : ""
|
|
|
178 |
|
|
|
179 |
//if !title.isEmpty && !body.isEmpty {
|
|
|
180 |
|
|
|
181 |
// var userNotification = UserNotificationModel()
|
|
|
182 |
// userNotification.title = title
|
|
|
183 |
// userNotification.description = body
|
|
|
184 |
//}
|
|
|
185 |
//}
|
|
|
186 |
//let title = aps["title"] as! String
|
|
|
187 |
//let body = aps["body"] as! String
|
|
|
188 |
|
|
|
189 |
// Print full message.
|
|
|
190 |
//print("userInfo title: \(title)")
|
|
|
191 |
//print("userInfo body: \(body)")
|
|
|
192 |
|
|
|
193 |
if let new_capsules = userInfo["new_capsules"] as? Int {
|
|
|
194 |
print("new_capsules : \(new_capsules)") // output: "some-value"
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/*
|
|
|
199 |
message userNotificationCenter iOS 10
|
|
|
200 |
[AnyHashable("aps"): {
|
|
|
201 |
alert = {
|
|
|
202 |
body = "Cuerpo notificaci\U00f3n";
|
|
|
203 |
subtitle = "Sub t\U00edtulo notificaci\U00f3n";
|
|
|
204 |
title = "T\U00edtulo notificaci\U00f3n";
|
|
|
205 |
};
|
|
|
206 |
}]
|
|
|
207 |
aps: ["alert": {
|
|
|
208 |
body = "Cuerpo notificaci\U00f3n";
|
|
|
209 |
subtitle = "Sub t\U00edtulo notificaci\U00f3n";
|
|
|
210 |
title = "T\U00edtulo notificaci\U00f3n";
|
|
|
211 |
}]
|
|
|
212 |
*/
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
| 1 |
efrain |
217 |
// Change this to your preferred presentation option
|
|
|
218 |
completionHandler([[.alert, .sound]])
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
|
222 |
didReceive response: UNNotificationResponse,
|
|
|
223 |
withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
print("message userNotificationCenter")
|
|
|
227 |
let userInfo = response.notification.request.content.userInfo
|
|
|
228 |
|
|
|
229 |
// ...
|
|
|
230 |
|
|
|
231 |
// With swizzling disabled you must let Messaging know about the message, for Analytics
|
|
|
232 |
// Messaging.messaging().appDidReceiveMessage(userInfo)
|
|
|
233 |
|
|
|
234 |
// Print full message.
|
| 8 |
efrain |
235 |
print("userInfo : \(userInfo)")
|
| 1 |
efrain |
236 |
|
|
|
237 |
completionHandler()
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
extension AppDelegate: MessagingDelegate {
|
|
|
245 |
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
|
|
|
246 |
|
|
|
247 |
Messaging.messaging().token { token, error in
|
|
|
248 |
if let error = error {
|
|
|
249 |
print("Error fetching FCM registration token: \(error)")
|
|
|
250 |
} else if let token = token {
|
|
|
251 |
let userInfo = ["token": token]
|
|
|
252 |
NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN , object: self, userInfo: userInfo)
|
|
|
253 |
print("FCM REGISTRATION TOKEN: \(token)")
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|