Rev 9 | Rev 17 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
//// AppDelegate.swift// twogetskills//// Created by Efrain Yanez Recanatini on 2/23/22.//import Foundationimport Firebaseimport Messagesimport BackgroundTasks//UIResponder, UIApplicationDelegate {class AppDelegate : NSObject, UIApplicationDelegate {private var isSyncInProgress = falseprivate var syncAdapter = SyncAdapter()private let gcmMessageIDKey = "gcm.message_id"private let appDao = AppDao.sharedInstancestatic var orientationLock = UIInterfaceOrientationMask.portraitfunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {return AppDelegate.orientationLock}func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {let database = Database.sharedInstanceif database.open() != nil {database.createTables()}let appDao = AppDao.sharedInstancevar appData = appDao.selectOne()if appData.id == 0 {appData.deviceUuid = "-"appDao.insert(model: appData)}print("registro del dispositivo")NotificationCenter.default.addObserver(self, selector: #selector(receivingNewDevice(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE, object: nil)if let uuid = UIDevice.current.identifierForVendor?.uuidString {let userInfo = ["uuid": uuid]NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_DEVICE , object: self, userInfo: userInfo)}NotificationCenter.default.addObserver(self, selector: #selector(receivingNewToken(n:)), name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN, object: nil)// Remote Notification Enable// Use Firebase library to configure APIsprint("Firebase configuracion")FirebaseApp.configure()UNUserNotificationCenter.current().delegate = selflet authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]UNUserNotificationCenter.current().requestAuthorization(options: authOptions,completionHandler: {_, _ in })Messaging.messaging().delegate = selfprint("Register For Remote Notifications")application.registerForRemoteNotifications()return true;}// Storage of the UID once the notification is received@objc func receivingNewDevice(n: NSNotification){print("receivingNewDevice")if n.userInfo != nil{if let new_uuid = n.userInfo?["uuid"]! as? String {var appData = appDao.selectOne()let old_uuid = appData.deviceUuidif new_uuid != old_uuid {var sync = SyncModel()sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICEsync.data = new_uuidif SyncDao.sharedInstance.insert(record: sync) > 0 {appData.deviceUuid = new_uuidprint("update query : 18")appDao.update(model: appData)syncAdapter.sync {success in}}}}}}@objc func receivingNewToken(n: NSNotification){if n.userInfo != nil{if let token = n.userInfo?["token"]! as? String {if !token.isEmpty {var appData = appDao.selectOne()if appData.fcmToken != token {var sync = SyncModel()sync.data = tokensync.type = Constants.SYNC_ADAPTER_TYPE_FCMif SyncDao.sharedInstance.insert(record: sync) > 0 {appData.fcmToken = tokenprint("update query : 19")appDao.update(model: appData)syncAdapter.sync {success in}}}}}}}}//Recibir mensajes@available(iOS 10, *)extension AppDelegate: UNUserNotificationCenterDelegate {// Receive displayed notifications for iOS 10 devices.func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)-> Void) {print("message userNotificationCenter iOS 10")let userInfo = notification.request.content.userInfo// With swizzling disabled you must let Messaging know about the message, for Analytics// Messaging.messaging().appDidReceiveMessage(userInfo)// ...print(userInfo)if let aps = userInfo["aps"] as? [String:Any] {//let badgeNumber = aps["badge"] as! Int//application.applicationIconBadgeNumber = badgeNumber//print("aps: \(aps)")//if let alert = aps["alert"]as? [String:Any] {// let title = alert["title"] ? alert["title"]! : ""// let body = alert["body"] ? alert["body"]! : ""//if !title.isEmpty && !body.isEmpty {// var userNotification = UserNotificationModel()// userNotification.title = title// userNotification.description = body//}//}//let title = aps["title"] as! String//let body = aps["body"] as! String// Print full message.//print("userInfo title: \(title)")//print("userInfo body: \(body)")if let new_capsules = userInfo["new_capsules"] as? Int {print("new_capsules : \(new_capsules)") // output: "some-value"}}/*message userNotificationCenter iOS 10[AnyHashable("aps"): {alert = {body = "Cuerpo notificaci\U00f3n";subtitle = "Sub t\U00edtulo notificaci\U00f3n";title = "T\U00edtulo notificaci\U00f3n";};}]aps: ["alert": {body = "Cuerpo notificaci\U00f3n";subtitle = "Sub t\U00edtulo notificaci\U00f3n";title = "T\U00edtulo notificaci\U00f3n";}]*/// Change this to your preferred presentation optioncompletionHandler([[.alert, .sound]])}func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {print("message userNotificationCenter")let userInfo = response.notification.request.content.userInfo// ...// With swizzling disabled you must let Messaging know about the message, for Analytics// Messaging.messaging().appDidReceiveMessage(userInfo)// Print full message.print("userInfo : \(userInfo)")completionHandler()}}extension AppDelegate: MessagingDelegate {func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {Messaging.messaging().token { token, error inif let error = error {print("Error fetching FCM registration token: \(error)")} else if let token = token {let userInfo = ["token": token]NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_REGISTER_NEW_FCM_TOKEN , object: self, userInfo: userInfo)print("FCM REGISTRATION TOKEN: \(token)")}}}}