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.

250 lines
7.0 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
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. GMEDB
  77. .shared
  78. .app
  79. .remove([.isOpenedTokenRenwalAlert, .isOpenedPopupNotification])
  80. StoreReviewHelper.shared.incrementAppOpenedCount()
  81. return true
  82. }
  83. func application(
  84. _ application: UIApplication,
  85. didReceiveRemoteNotification userInfo: [AnyHashable: Any]
  86. ) { }
  87. func application(
  88. _ application: UIApplication,
  89. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  90. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  91. ) {
  92. }
  93. func application(
  94. _ application: UIApplication,
  95. didFailToRegisterForRemoteNotificationsWithError error: Error
  96. ) { }
  97. func application(
  98. _ application: UIApplication,
  99. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  100. ) {
  101. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  102. print("APNs device token: \(deviceTokenString)")
  103. Messaging.messaging().apnsToken = deviceToken
  104. }
  105. func applicationDidEnterBackground(_ application: UIApplication) {
  106. let taskID = application.beginBackgroundTask(expirationHandler: nil)
  107. let language = Localize.currentLanguage()
  108. GMEDB.shared.app.set(language, .currentLanguage)
  109. let viewcontroller = UIViewController()
  110. viewcontroller.view.backgroundColor = .themeRed
  111. viewcontroller.view.frame = UIScreen.main.bounds
  112. overlayView = viewcontroller.view
  113. guard let view = overlayView else {return}
  114. let topView = UIApplication.shared.keyWindow?.subviews.last
  115. topView?.addSubview(view)
  116. topView?.bringSubviewToFront(view)
  117. if taskID != UIBackgroundTaskIdentifier.invalid {
  118. UIApplication.shared.endBackgroundTask(taskID)
  119. }
  120. }
  121. func applicationWillEnterForeground(_ application: UIApplication) {
  122. if let language = GMEDB.shared.app.string(.currentLanguage) {
  123. Localize.setCurrentLanguage(language)
  124. }
  125. overlayView?.removeFromSuperview()
  126. }
  127. func application(
  128. _ app: UIApplication,
  129. open url: URL,
  130. options: [UIApplication.OpenURLOptionsKey : Any] = [:]
  131. ) -> Bool {
  132. return true
  133. }
  134. }
  135. // MARK: - UNUserNotificationCenterDelegate
  136. extension AppDelegate: UNUserNotificationCenterDelegate {
  137. func userNotificationCenter(
  138. _ center: UNUserNotificationCenter,
  139. willPresent notification: UNNotification,
  140. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  141. ) {
  142. let userInfo = notification.request.content.userInfo
  143. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  144. print(userInfo)
  145. completionHandler([
  146. UNNotificationPresentationOptions.alert,
  147. UNNotificationPresentationOptions.sound,
  148. UNNotificationPresentationOptions.badge
  149. ])
  150. }
  151. func userNotificationCenter(
  152. _ center: UNUserNotificationCenter,
  153. didReceive response: UNNotificationResponse,
  154. withCompletionHandler completionHandler: @escaping () -> Void
  155. ) {
  156. let userInfo = response.notification.request.content.userInfo
  157. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  158. // open push notification's destination
  159. guard
  160. window?.rootViewController is LGSideMenuController,
  161. let destination = extractDestination(userInfo: userInfo as? [String: AnyObject]) else {
  162. completionHandler()
  163. return
  164. }
  165. let baseVC = window?.rootViewController?.presentedViewController ?? window?.rootViewController
  166. DispatchQueue.main.async {
  167. switch destination {
  168. case .trasactionHistory:
  169. TransactionHistoryGroupWireframe().open(overseasType: .inbound, on: baseVC)
  170. case .pushNotification:
  171. NotificationHistoryWireframe().open(on: baseVC)
  172. }
  173. }
  174. completionHandler()
  175. }
  176. }
  177. extension AppDelegate {
  178. private func extractDestination(userInfo: [String : AnyObject]?) -> PushNotificationDestination? {
  179. guard
  180. let destination = userInfo?["destination"] as? String,
  181. let destinationType = PushNotificationDestination.getDestination(target: destination) else {
  182. return nil
  183. }
  184. return destinationType
  185. }
  186. }
  187. // MARK: - MessagingDelegate
  188. extension AppDelegate: MessagingDelegate {
  189. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  190. print("Firebase registration token: \(fcmToken)")
  191. let userInfo = ["token": fcmToken]
  192. NotificationCenter.default.post(
  193. name: Notification.Name("FCMToken"),
  194. object: nil,
  195. userInfo: userInfo
  196. )
  197. }
  198. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  199. print("Received data message: \(remoteMessage.appData)")
  200. }
  201. }