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.

379 lines
16 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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 LGSideMenuController
  20. let isDevelopment = true
  21. @UIApplicationMain
  22. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
  23. var window: UIWindow?
  24. let gcmMessageIDKey = "gcm.message_id"
  25. var firstTranscationStatusForHome:Bool?
  26. var firstTranscationStatusForProfileForm:Bool?
  27. var cmRegistrationId: String?
  28. var notificationSet: Box<Bool?> = Box(nil)
  29. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  30. UIApplication.shared.applicationIconBadgeNumber = 0
  31. firstTranscationStatusForHome = true
  32. firstTranscationStatusForProfileForm = true
  33. NetworkActivityLogger.shared.startLogging()
  34. NetworkActivityLogger.shared.level = .debug
  35. IQKeyboardManager.shared.enable = true
  36. FirebaseApp.configure()
  37. Fabric.with([Crashlytics.self])
  38. Messaging.messaging().delegate = self
  39. registerForNotification(application: application)
  40. setUUID()
  41. setupNavBar()
  42. setupStatusBar()
  43. setEntryPoint()
  44. return true
  45. }
  46. private func setUUID() {
  47. if UserDefaults.standard.string(forKey: AppConstants.uuid) == nil {
  48. let uuid = UUID().uuidString
  49. UserDefaults.standard.set(uuid, forKey: AppConstants.uuid)
  50. }
  51. }
  52. private func setupStatusBar() {
  53. // let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
  54. // if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
  55. // statusBar.backgroundColor = UIColor.blue
  56. // }
  57. }
  58. private func setupNavBar() {
  59. let appearance = UINavigationBar.appearance()
  60. appearance.backIndicatorImage = #imageLiteral(resourceName: "backIconBlack")
  61. appearance.backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "backIconBlack")
  62. appearance.tintColor = UIColor.black
  63. }
  64. private func setEntryPoint() {
  65. let _default = UserDefaults.standard
  66. if let loginStatus = _default.string(forKey: UserKeys.accessCode) {
  67. // user is logged in
  68. let mainWireFrame = MainWireframe.shared
  69. // self.window?.rootViewController =
  70. let tabBarViewController = mainWireFrame?.getMainView()
  71. let sidemenuVc = UIStoryboard(name: "_Home", bundle: nil).instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
  72. //
  73. let sideMenuController = LGSideMenuController(rootViewController: tabBarViewController, leftViewController: sidemenuVc, rightViewController: nil)
  74. //
  75. sideMenuController.rootViewLayerShadowColor = UIColor(white: 0.9, alpha: 0.6)
  76. sideMenuController.rootViewLayerShadowRadius = 8.0
  77. sideMenuController.leftViewPresentationStyle = .scaleFromBig
  78. sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 70.0
  79. window?.backgroundColor = sidemenuVc.view.backgroundColor
  80. self.window?.rootViewController = sideMenuController
  81. }else {
  82. // go to splashscreen
  83. let splashWireframe = SplashScreenWireframe()
  84. let nav = UINavigationController.init(rootViewController: splashWireframe.getMainView())
  85. self.window?.rootViewController = nav
  86. }
  87. }
  88. private func registerForNotification(application: UIApplication) {
  89. if #available(iOS 10.0, *) {
  90. // For iOS 10 display notification (sent via APNS)
  91. UNUserNotificationCenter.current().delegate = self
  92. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  93. UNUserNotificationCenter.current().requestAuthorization(
  94. options: authOptions,
  95. completionHandler: {_, _ in })
  96. } else {
  97. let settings: UIUserNotificationSettings =
  98. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  99. application.registerUserNotificationSettings(settings)
  100. }
  101. application.registerForRemoteNotifications()
  102. }
  103. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  104. // If you are receiving a notification message while your app is in the background,
  105. // this callback will not be fired till the user taps on the notification launching the application.
  106. // TODO: Handle data of notification
  107. // With swizzling disabled you must let Messaging know about the message, for Analytics
  108. // Messaging.messaging().appDidReceiveMessage(userInfo)
  109. // Print message ID.
  110. if userInfo[gcmMessageIDKey] != nil {
  111. // print(" message: \(messageID)")
  112. }
  113. // Print full message.
  114. // print("fore ground ", userInfo)
  115. }
  116. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  117. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  118. // If you are receiving a notification message while your app is in the background,
  119. // this callback will not be fired till the user taps on the notification launching the application.
  120. // TODO: Handle data of notification
  121. // With swizzling disabled you must let Messaging know about the message, for Analytics
  122. // Messaging.messaging().appDidReceiveMessage(userInfo)
  123. // Print message ID.
  124. if userInfo[gcmMessageIDKey] != nil {
  125. // print("Message ID completionHandler: \(messageID)")
  126. }
  127. // Print full message.
  128. // print(userInfo)
  129. /**
  130. redirection of page after background push notification
  131. */
  132. if
  133. let aps = userInfo["aps"] as? NSDictionary,
  134. let alert = aps["alert"] as? NSDictionary,
  135. let _ = alert["body"] as? String,
  136. let _ = alert["title"] as? String {
  137. let codeID = userInfo["code"] as? String
  138. self.redirectWithPushNotification(notificationCode: codeID!)
  139. }
  140. completionHandler(UIBackgroundFetchResult.newData)
  141. }
  142. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  143. cmRegistrationId = fcmToken
  144. notificationSet.value = true
  145. print(fcmToken)
  146. }
  147. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  148. print(remoteMessage.appData)
  149. }
  150. public func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  151. }
  152. @available(iOS 10.0, *)
  153. func userNotificationCenter(_ center: UNUserNotificationCenter,
  154. willPresent notification: UNNotification,
  155. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  156. var userInfo = notification.request.content.userInfo
  157. if userInfo[gcmMessageIDKey] != nil {
  158. // print("Message ID: def \(messageID)")
  159. }
  160. /**
  161. redirection of page after foreground push notification
  162. */
  163. if
  164. let aps = userInfo["aps"] as? NSDictionary,
  165. let alert = aps["alert"] as? NSDictionary,
  166. let body = alert["body"] as? String,
  167. let title = alert["title"] as? String {
  168. // print("Title: \(title) \nBody:\(body)")
  169. let codeID = userInfo["code"] as? String
  170. let banner = Banner(title: title, subtitle: body, image: #imageLiteral(resourceName: "ic_gme"), backgroundColor: UIColor(red:0.93, green:0.11, blue:0.14, alpha:1.0))
  171. banner.didTapBlock = {
  172. // print("Banner was tapped on \(Date())!")
  173. self.redirectWithPushNotification(notificationCode: codeID!)
  174. }
  175. banner.show(duration: 5.0)
  176. }
  177. if userInfo["code"] != nil {
  178. // print("codeAP delegate: \(codeID)")
  179. }
  180. // Change this to your preferred presentation option
  181. completionHandler([])
  182. }
  183. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  184. // print("Unable to register for remote notifications: \(error.localizedDescription)")
  185. }
  186. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  187. }
  188. /**
  189. method for redirection of page when push notification tap
  190. */
  191. func redirectWithPushNotification(notificationCode: String){
  192. let nav = UINavigationController()
  193. switch notificationCode {
  194. case "500":
  195. InviteViewController.notificationCode = "p500"
  196. let storyboard = UIStoryboard(name: "Invite", bundle: nil)
  197. let viewController = storyboard.instantiateViewController(withIdentifier :"InviteViewController") as! InviteViewController
  198. let navController = UINavigationController.init(rootViewController: viewController)
  199. if let window = self.window, let rootViewController = window.rootViewController {
  200. var currentController = rootViewController
  201. while let presentedController = currentController.presentedViewController {
  202. currentController = presentedController
  203. }
  204. currentController.present(navController, animated: true, completion: nil)
  205. }
  206. case "200":
  207. TranscationStatementViewController.notificationCode = "p200"
  208. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  209. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  210. nav.viewControllers = [transcationStatementViewController]
  211. window?.rootViewController = nav
  212. }
  213. case "201":
  214. MoneyRequestViewController.notificationCode = "p201"
  215. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  216. if let moneyRequestViewController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as? MoneyRequestViewController {
  217. nav.viewControllers = [moneyRequestViewController]
  218. window?.rootViewController = nav
  219. }
  220. case "202":
  221. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "WalletTransactionListViewController")
  222. WalletTransactionListViewController.notificationCode = "p202"
  223. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  224. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  225. walletTransactionListViewController.walletStatus = "walletBorrow"
  226. nav.viewControllers = [walletTransactionListViewController]
  227. window?.rootViewController = nav
  228. }
  229. case "203":
  230. TranscationStatementViewController.notificationCode = "p203"
  231. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  232. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  233. nav.viewControllers = [transcationStatementViewController]
  234. window?.rootViewController = nav
  235. }
  236. case "204":
  237. TranscationStatementViewController.notificationCode = "p204"
  238. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  239. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  240. nav.viewControllers = [transcationStatementViewController]
  241. window?.rootViewController = nav
  242. }
  243. case "205":
  244. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "walletViewController")
  245. WalletTransactionListViewController.notificationCode = "p205"
  246. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  247. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  248. walletTransactionListViewController.walletStatus = "walletTransfer"
  249. nav.viewControllers = [walletTransactionListViewController]
  250. window?.rootViewController = nav
  251. }
  252. case "206":
  253. TranscationStatementViewController.notificationCode = "p200"
  254. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  255. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  256. nav.viewControllers = [transcationStatementViewController]
  257. window?.rootViewController = nav
  258. }
  259. case "207":
  260. TranscationStatementViewController.notificationCode = "p200"
  261. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  262. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  263. nav.viewControllers = [transcationStatementViewController]
  264. window?.rootViewController = nav
  265. }
  266. case "208":
  267. TranscationStatementViewController.notificationCode = "p208"
  268. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  269. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  270. nav.viewControllers = [transcationStatementViewController]
  271. window?.rootViewController = nav
  272. }
  273. case "700":
  274. // redirectViewController(storyBoardName: "Reward", identifier: "RewardViewController")
  275. RewardViewController.notificationCode = "p700"
  276. let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main)
  277. if let rewardViewController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as? RewardViewController {
  278. nav.viewControllers = [rewardViewController]
  279. window?.rootViewController = nav
  280. }
  281. case "750":
  282. return
  283. //redirectViewController(storyBoardName: "Home", identifier: "CommentsViewController")
  284. default:
  285. return
  286. }
  287. }
  288. }