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.

265 lines
7.4 KiB

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