// // AppDelegate.swift // GMERemittance // // Created by Fm-user on 11/30/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Firebase import FirebaseAuth import UserNotifications import FirebaseInstanceID import FirebaseMessaging import BRYXBanner import Fabric import Crashlytics import AlamofireNetworkActivityLogger import IQKeyboardManagerSwift import Localize_Swift import ChannelIO let server: Server = .live var overlayView: UIView? @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { UIApplication.shared.applicationIconBadgeNumber = 0 #if DEBUG NetworkActivityLogger.shared.startLogging() NetworkActivityLogger.shared.level = .debug #endif IQKeyboardManager.shared.enable = true IQKeyboardManager.shared.shouldResignOnTouchOutside = true FirebaseApp.configure() Fabric.with([Crashlytics.self]) Fabric.sharedSDK().debug = true Messaging.messaging().delegate = self let center = UNUserNotificationCenter.current() center.delegate = self center.requestAuthorization(options: [.alert, .sound, .badge]) { (_, _) in} application.registerForRemoteNotifications() // Initialize ChannelIO ChannelIO.initialize() self.window?.rootViewController = LauncherScreenWireframe().getMainView() GMEDB .shared .app .remove([.isOpenedTokenRenwalAlert, .isOpenedPopupNotification]) StoreReviewHelper.shared.incrementAppOpenedCount() return true } func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any] ) { } func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void ) { ChannelIO.handlePushNotification(userInfo) { completionHandler(.noData) } } func application( _ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error ) { } func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) print("APNs device token: \(deviceTokenString)") Messaging.messaging().apnsToken = deviceToken ChannelIO.initPushToken(deviceToken: deviceToken) } func applicationDidEnterBackground(_ application: UIApplication) { let language = Localize.currentLanguage() GMEDB.shared.app.set(language, .currentLanguage) let viewcontroller = UIViewController() viewcontroller.view.backgroundColor = .themeRed viewcontroller.view.frame = UIScreen.main.bounds overlayView = viewcontroller.view guard let view = overlayView else {return} let topView = UIApplication.shared.keyWindow?.subviews.last topView?.addSubview(view) topView?.bringSubviewToFront(view) } func applicationWillEnterForeground(_ application: UIApplication) { if let language = GMEDB.shared.app.string(.currentLanguage) { Localize.setCurrentLanguage(language) } overlayView?.removeFromSuperview() } } // MARK: - UNUserNotificationCenterDelegate extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void ) { let userInfo = notification.request.content.userInfo print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")") completionHandler([ UNNotificationPresentationOptions.alert, UNNotificationPresentationOptions.sound, UNNotificationPresentationOptions.badge ]) } func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void ) { let userInfo = response.notification.request.content.userInfo print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")") if ChannelIO.isChannelPushNotification(userInfo) { ChannelIO.handlePushNotification(userInfo) } print(userInfo) completionHandler() } } // MARK: - MessagingDelegate extension AppDelegate: MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") let userInfo = ["token": fcmToken] NotificationCenter.default.post( name: Notification.Name("FCMToken"), object: nil, userInfo: userInfo ) } func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { print("Received data message: \(remoteMessage.appData)") } }