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.

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