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.

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