You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.2 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
  1. //
  2. // AppDelegate.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 11/30/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Firebase
  10. import FirebaseAuth
  11. import UserNotifications
  12. import FirebaseInstanceID
  13. import FirebaseMessaging
  14. import BRYXBanner
  15. import Fabric
  16. import Crashlytics
  17. import AlamofireNetworkActivityLogger
  18. import IQKeyboardManagerSwift
  19. import LGSideMenuController
  20. import Localize_Swift
  21. import ChannelIO
  22. let server: Server = .staging
  23. var overlayView: UIView?
  24. @UIApplicationMain
  25. class AppDelegate: UIResponder, UIApplicationDelegate {
  26. var window: UIWindow?
  27. func application(
  28. _ application: UIApplication,
  29. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  30. ) -> Bool {
  31. UIApplication.shared.applicationIconBadgeNumber = 0
  32. NetworkActivityLogger.shared.startLogging()
  33. NetworkActivityLogger.shared.level = .debug
  34. IQKeyboardManager.shared.enable = true
  35. IQKeyboardManager.shared.shouldResignOnTouchOutside = true
  36. FirebaseApp.configure()
  37. Fabric.with([Crashlytics.self])
  38. Fabric.sharedSDK().debug = true
  39. Messaging.messaging().delegate = self
  40. let center = UNUserNotificationCenter.current()
  41. center.delegate = self
  42. center.requestAuthorization(options: [.alert, .sound, .badge]) { (_, _) in}
  43. application.registerForRemoteNotifications()
  44. print("FCM Token: \(Messaging.messaging().fcmToken ?? "nil")")
  45. // Initialize ChannelIO
  46. ChannelIO.initialize()
  47. self.window?.rootViewController = LauncherScreenWireframe().getMainView()
  48. GMEDB
  49. .shared
  50. .app
  51. .remove([.isOpenedTokenRenwalAlert, .isOpenedPopupNotification])
  52. // FIXME: For Test
  53. GMEDB.shared.user.set("autodebit", .redirectTo)
  54. return true
  55. }
  56. func application(
  57. _ application: UIApplication,
  58. didReceiveRemoteNotification userInfo: [AnyHashable: Any]
  59. ) { }
  60. func application(
  61. _ application: UIApplication,
  62. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  63. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  64. ) {
  65. ChannelIO.handlePushNotification(userInfo) {
  66. completionHandler(.noData)
  67. }
  68. }
  69. func application(
  70. _ application: UIApplication,
  71. didFailToRegisterForRemoteNotificationsWithError error: Error
  72. ) { }
  73. func application(
  74. _ application: UIApplication,
  75. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  76. ) {
  77. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  78. print("APNs device token: \(deviceTokenString)")
  79. Messaging.messaging().apnsToken = deviceToken
  80. ChannelIO.initPushToken(deviceToken: deviceToken)
  81. }
  82. func applicationDidEnterBackground(_ application: UIApplication) {
  83. let language = Localize.currentLanguage()
  84. GMEDB.shared.app.set(language, .currentLanguage)
  85. let viewcontroller = UIViewController()
  86. viewcontroller.view.backgroundColor = AppConstants.themeRedColor
  87. viewcontroller.view.frame = UIScreen.main.bounds
  88. overlayView = viewcontroller.view
  89. guard let view = overlayView else {return}
  90. let topView = UIApplication.shared.keyWindow?.subviews.last
  91. topView?.addSubview(view)
  92. topView?.bringSubviewToFront(view)
  93. }
  94. func applicationWillEnterForeground(_ application: UIApplication) {
  95. if let language = GMEDB.shared.app.string(.currentLanguage) {
  96. Localize.setCurrentLanguage(language)
  97. }
  98. overlayView?.removeFromSuperview()
  99. }
  100. }
  101. // MARK: - UNUserNotificationCenterDelegate
  102. extension AppDelegate: UNUserNotificationCenterDelegate {
  103. func userNotificationCenter(
  104. _ center: UNUserNotificationCenter,
  105. willPresent notification: UNNotification,
  106. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  107. ) {
  108. let userInfo = notification.request.content.userInfo
  109. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  110. completionHandler([
  111. UNNotificationPresentationOptions.alert,
  112. UNNotificationPresentationOptions.sound,
  113. UNNotificationPresentationOptions.badge
  114. ])
  115. }
  116. func userNotificationCenter(
  117. _ center: UNUserNotificationCenter,
  118. didReceive response: UNNotificationResponse,
  119. withCompletionHandler completionHandler: @escaping () -> Void
  120. ) {
  121. let userInfo = response.notification.request.content.userInfo
  122. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  123. if ChannelIO.isChannelPushNotification(userInfo) {
  124. ChannelIO.handlePushNotification(userInfo)
  125. }
  126. print(userInfo)
  127. completionHandler()
  128. }
  129. }
  130. // MARK: - MessagingDelegate
  131. extension AppDelegate: MessagingDelegate {
  132. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  133. print("Firebase registration token: \(fcmToken)")
  134. let userInfo = ["token": fcmToken]
  135. NotificationCenter.default.post(
  136. name: Notification.Name("FCMToken"),
  137. object: nil,
  138. userInfo: userInfo
  139. )
  140. }
  141. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  142. print("Received data message: \(remoteMessage.appData)")
  143. }
  144. }