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.

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