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.

257 lines
7.3 KiB

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
4 years ago
6 years ago
4 years ago
5 years ago
5 years ago
5 years ago
6 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 Localize_Swift
  20. import LGSideMenuController
  21. #if DEBUG
  22. var server: Server = .live
  23. #else
  24. let server: Server = .live
  25. #endif
  26. var destination: PushNotificationDestination?
  27. var overlayView: UIView?
  28. enum PushNotificationDestination {
  29. case trasactionHistory
  30. case pushNotification
  31. static func getDestination(target: String) -> PushNotificationDestination? {
  32. switch target {
  33. case "TransactionHistory":
  34. return .trasactionHistory
  35. case "PushNotification":
  36. return .pushNotification
  37. default:
  38. return nil
  39. }
  40. }
  41. }
  42. @UIApplicationMain
  43. class AppDelegate: UIResponder, UIApplicationDelegate {
  44. var window: UIWindow?
  45. func application(
  46. _ application: UIApplication,
  47. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  48. ) -> Bool {
  49. UIApplication.shared.applicationIconBadgeNumber = 0
  50. #if DEBUG
  51. NetworkActivityLogger.shared.startLogging()
  52. NetworkActivityLogger.shared.level = .debug
  53. #endif
  54. IQKeyboardManager.shared.enable = true
  55. IQKeyboardManager.shared.shouldResignOnTouchOutside = true
  56. IQKeyboardManager.shared.toolbarTintColor = .black
  57. FirebaseApp.configure()
  58. Fabric.with([Crashlytics.self])
  59. Fabric.sharedSDK().debug = true
  60. Messaging.messaging().delegate = self
  61. let center = UNUserNotificationCenter.current()
  62. center.delegate = self
  63. center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
  64. guard granted else { return }
  65. center.getNotificationSettings { settings in
  66. print("Notification settings: \(settings)")
  67. DispatchQueue.main.async {
  68. UIApplication.shared.registerForRemoteNotifications()
  69. }
  70. }
  71. }
  72. // Move push notification's destination
  73. let useInfo = launchOptions?[.remoteNotification] as? [String: AnyObject]
  74. destination = extractDestination(userInfo: useInfo)
  75. self.window?.rootViewController = LauncherScreenWireframe().getMainView()
  76. if #available(iOS 13.0, *) {
  77. window?.overrideUserInterfaceStyle = .light
  78. }
  79. GMEDB
  80. .shared
  81. .app
  82. .remove([.isOpenedTokenRenwalAlert, .isOpenedPopupNotification])
  83. StoreReviewHelper.shared.incrementAppOpenedCount()
  84. //Default "Back title is removed"
  85. let barButtonItemAppearance = UIBarButtonItem.appearance()
  86. barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -1000, vertical: 0), for:UIBarMetrics.default)
  87. return true
  88. }
  89. func application(
  90. _ application: UIApplication,
  91. didReceiveRemoteNotification userInfo: [AnyHashable: Any]
  92. ) { }
  93. func application(
  94. _ application: UIApplication,
  95. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  96. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  97. ) {
  98. }
  99. func application(
  100. _ application: UIApplication,
  101. didFailToRegisterForRemoteNotificationsWithError error: Error
  102. ) { }
  103. func application(
  104. _ application: UIApplication,
  105. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  106. ) {
  107. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  108. print("APNs device token: \(deviceTokenString)")
  109. Messaging.messaging().apnsToken = deviceToken
  110. }
  111. func applicationDidEnterBackground(_ application: UIApplication) {
  112. let taskID = application.beginBackgroundTask(expirationHandler: nil)
  113. let language = Localize.currentLanguage()
  114. GMEDB.shared.app.set(language, .currentLanguage)
  115. let viewcontroller = UIViewController()
  116. viewcontroller.view.backgroundColor = .themeRed
  117. viewcontroller.view.frame = UIScreen.main.bounds
  118. overlayView = viewcontroller.view
  119. guard let view = overlayView else {return}
  120. let topView = UIApplication.shared.keyWindow?.subviews.last
  121. topView?.addSubview(view)
  122. topView?.bringSubviewToFront(view)
  123. if taskID != UIBackgroundTaskIdentifier.invalid {
  124. UIApplication.shared.endBackgroundTask(taskID)
  125. }
  126. }
  127. func applicationWillEnterForeground(_ application: UIApplication) {
  128. if let language = GMEDB.shared.app.string(.currentLanguage) {
  129. Localize.setCurrentLanguage(language)
  130. }
  131. overlayView?.removeFromSuperview()
  132. }
  133. func application(
  134. _ app: UIApplication,
  135. open url: URL,
  136. options: [UIApplication.OpenURLOptionsKey : Any] = [:]
  137. ) -> Bool {
  138. return true
  139. }
  140. }
  141. // MARK: - UNUserNotificationCenterDelegate
  142. extension AppDelegate: UNUserNotificationCenterDelegate {
  143. func userNotificationCenter(
  144. _ center: UNUserNotificationCenter,
  145. willPresent notification: UNNotification,
  146. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  147. ) {
  148. let userInfo = notification.request.content.userInfo
  149. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  150. print(userInfo)
  151. completionHandler([
  152. UNNotificationPresentationOptions.alert,
  153. UNNotificationPresentationOptions.sound,
  154. UNNotificationPresentationOptions.badge
  155. ])
  156. }
  157. func userNotificationCenter(
  158. _ center: UNUserNotificationCenter,
  159. didReceive response: UNNotificationResponse,
  160. withCompletionHandler completionHandler: @escaping () -> Void
  161. ) {
  162. let userInfo = response.notification.request.content.userInfo
  163. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  164. // open push notification's destination
  165. guard
  166. window?.rootViewController is LGSideMenuController,
  167. let destination = extractDestination(userInfo: userInfo as? [String: AnyObject]) else {
  168. completionHandler()
  169. return
  170. }
  171. let baseVC = window?.rootViewController?.presentedViewController ?? window?.rootViewController
  172. DispatchQueue.main.async {
  173. switch destination {
  174. case .trasactionHistory:
  175. TransactionHistoryGroupWireframe().open(overseasType: .inbound, on: baseVC)
  176. case .pushNotification:
  177. NotificationHistoryWireframe().open(on: baseVC)
  178. }
  179. }
  180. completionHandler()
  181. }
  182. }
  183. extension AppDelegate {
  184. private func extractDestination(userInfo: [String : AnyObject]?) -> PushNotificationDestination? {
  185. guard
  186. let destination = userInfo?["destination"] as? String,
  187. let destinationType = PushNotificationDestination.getDestination(target: destination) else {
  188. return nil
  189. }
  190. return destinationType
  191. }
  192. }
  193. // MARK: - MessagingDelegate
  194. extension AppDelegate: MessagingDelegate {
  195. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  196. print("Firebase registration token: \(fcmToken)")
  197. let userInfo = ["token": fcmToken]
  198. NotificationCenter.default.post(
  199. name: Notification.Name("FCMToken"),
  200. object: nil,
  201. userInfo: userInfo
  202. )
  203. }
  204. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  205. print("Received data message: \(remoteMessage.appData)")
  206. }
  207. }