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.

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