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.

381 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: "SideMenu", 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. sideMenuController.leftViewBackgroundBlurEffect = UIBlurEffect(style: .regular)
  80. window?.backgroundColor = sidemenuVc.view.backgroundColor
  81. self.window?.rootViewController = sideMenuController
  82. }else {
  83. // go to splashscreen
  84. let splashWireframe = SplashScreenWireframe()
  85. let nav = UINavigationController.init(rootViewController: splashWireframe.getMainView())
  86. self.window?.rootViewController = nav
  87. }
  88. }
  89. private func registerForNotification(application: UIApplication) {
  90. if #available(iOS 10.0, *) {
  91. // For iOS 10 display notification (sent via APNS)
  92. UNUserNotificationCenter.current().delegate = self
  93. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  94. UNUserNotificationCenter.current().requestAuthorization(
  95. options: authOptions,
  96. completionHandler: {_, _ in })
  97. } else {
  98. let settings: UIUserNotificationSettings =
  99. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  100. application.registerUserNotificationSettings(settings)
  101. }
  102. application.registerForRemoteNotifications()
  103. }
  104. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  105. // If you are receiving a notification message while your app is in the background,
  106. // this callback will not be fired till the user taps on the notification launching the application.
  107. // TODO: Handle data of notification
  108. // With swizzling disabled you must let Messaging know about the message, for Analytics
  109. // Messaging.messaging().appDidReceiveMessage(userInfo)
  110. // Print message ID.
  111. if userInfo[gcmMessageIDKey] != nil {
  112. // print(" message: \(messageID)")
  113. }
  114. // Print full message.
  115. // print("fore ground ", userInfo)
  116. }
  117. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  118. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  119. // If you are receiving a notification message while your app is in the background,
  120. // this callback will not be fired till the user taps on the notification launching the application.
  121. // TODO: Handle data of notification
  122. // With swizzling disabled you must let Messaging know about the message, for Analytics
  123. // Messaging.messaging().appDidReceiveMessage(userInfo)
  124. // Print message ID.
  125. if userInfo[gcmMessageIDKey] != nil {
  126. // print("Message ID completionHandler: \(messageID)")
  127. }
  128. // Print full message.
  129. // print(userInfo)
  130. /**
  131. redirection of page after background push notification
  132. */
  133. if
  134. let aps = userInfo["aps"] as? NSDictionary,
  135. let alert = aps["alert"] as? NSDictionary,
  136. let _ = alert["body"] as? String,
  137. let _ = alert["title"] as? String {
  138. let codeID = userInfo["code"] as? String
  139. self.redirectWithPushNotification(notificationCode: codeID!)
  140. }
  141. completionHandler(UIBackgroundFetchResult.newData)
  142. }
  143. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  144. cmRegistrationId = fcmToken
  145. notificationSet.value = true
  146. print(fcmToken)
  147. }
  148. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  149. print(remoteMessage.appData)
  150. }
  151. public func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  152. }
  153. @available(iOS 10.0, *)
  154. func userNotificationCenter(_ center: UNUserNotificationCenter,
  155. willPresent notification: UNNotification,
  156. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  157. var userInfo = notification.request.content.userInfo
  158. if userInfo[gcmMessageIDKey] != nil {
  159. // print("Message ID: def \(messageID)")
  160. }
  161. /**
  162. redirection of page after foreground push notification
  163. */
  164. if
  165. let aps = userInfo["aps"] as? NSDictionary,
  166. let alert = aps["alert"] as? NSDictionary,
  167. let body = alert["body"] as? String,
  168. let title = alert["title"] as? String {
  169. // print("Title: \(title) \nBody:\(body)")
  170. let codeID = userInfo["code"] as? String
  171. 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))
  172. banner.didTapBlock = {
  173. // print("Banner was tapped on \(Date())!")
  174. self.redirectWithPushNotification(notificationCode: codeID!)
  175. }
  176. banner.show(duration: 5.0)
  177. }
  178. if userInfo["code"] != nil {
  179. // print("codeAP delegate: \(codeID)")
  180. }
  181. // Change this to your preferred presentation option
  182. completionHandler([])
  183. }
  184. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  185. // print("Unable to register for remote notifications: \(error.localizedDescription)")
  186. }
  187. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  188. }
  189. /**
  190. method for redirection of page when push notification tap
  191. */
  192. func redirectWithPushNotification(notificationCode: String){
  193. let nav = UINavigationController()
  194. switch notificationCode {
  195. case "500":
  196. InviteViewController.notificationCode = "p500"
  197. let storyboard = UIStoryboard(name: "Invite", bundle: nil)
  198. let viewController = storyboard.instantiateViewController(withIdentifier :"InviteViewController") as! InviteViewController
  199. let navController = UINavigationController.init(rootViewController: viewController)
  200. if let window = self.window, let rootViewController = window.rootViewController {
  201. var currentController = rootViewController
  202. while let presentedController = currentController.presentedViewController {
  203. currentController = presentedController
  204. }
  205. currentController.present(navController, animated: true, completion: nil)
  206. }
  207. case "200":
  208. TranscationStatementViewController.notificationCode = "p200"
  209. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  210. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  211. nav.viewControllers = [transcationStatementViewController]
  212. window?.rootViewController = nav
  213. }
  214. case "201":
  215. MoneyRequestViewController.notificationCode = "p201"
  216. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  217. if let moneyRequestViewController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as? MoneyRequestViewController {
  218. nav.viewControllers = [moneyRequestViewController]
  219. window?.rootViewController = nav
  220. }
  221. case "202":
  222. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "WalletTransactionListViewController")
  223. WalletTransactionListViewController.notificationCode = "p202"
  224. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  225. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  226. walletTransactionListViewController.walletStatus = "walletBorrow"
  227. nav.viewControllers = [walletTransactionListViewController]
  228. window?.rootViewController = nav
  229. }
  230. case "203":
  231. TranscationStatementViewController.notificationCode = "p203"
  232. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  233. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  234. nav.viewControllers = [transcationStatementViewController]
  235. window?.rootViewController = nav
  236. }
  237. case "204":
  238. TranscationStatementViewController.notificationCode = "p204"
  239. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  240. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  241. nav.viewControllers = [transcationStatementViewController]
  242. window?.rootViewController = nav
  243. }
  244. case "205":
  245. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "walletViewController")
  246. WalletTransactionListViewController.notificationCode = "p205"
  247. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  248. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  249. walletTransactionListViewController.walletStatus = "walletTransfer"
  250. nav.viewControllers = [walletTransactionListViewController]
  251. window?.rootViewController = nav
  252. }
  253. case "206":
  254. TranscationStatementViewController.notificationCode = "p200"
  255. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  256. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  257. nav.viewControllers = [transcationStatementViewController]
  258. window?.rootViewController = nav
  259. }
  260. case "207":
  261. TranscationStatementViewController.notificationCode = "p200"
  262. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  263. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  264. nav.viewControllers = [transcationStatementViewController]
  265. window?.rootViewController = nav
  266. }
  267. case "208":
  268. TranscationStatementViewController.notificationCode = "p208"
  269. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  270. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  271. nav.viewControllers = [transcationStatementViewController]
  272. window?.rootViewController = nav
  273. }
  274. case "700":
  275. // redirectViewController(storyBoardName: "Reward", identifier: "RewardViewController")
  276. RewardViewController.notificationCode = "p700"
  277. let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main)
  278. if let rewardViewController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as? RewardViewController {
  279. nav.viewControllers = [rewardViewController]
  280. window?.rootViewController = nav
  281. }
  282. case "750":
  283. return
  284. //redirectViewController(storyBoardName: "Home", identifier: "CommentsViewController")
  285. default:
  286. return
  287. }
  288. }
  289. }