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.

280 lines
8.2 KiB

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