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.

443 lines
20 KiB

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. // Override point for customization after application launch.
  32. // [START set_messaging_delegate]
  33. Messaging.messaging().delegate = self
  34. // [END set_messaging_delegate]
  35. // Register for remote notifications. This shows a permission dialog on first run, to
  36. // show the dialog at a more appropriate time move this registration accordingly.
  37. // [START register_for_notifications]
  38. NetworkActivityLogger.shared.startLogging()
  39. NetworkActivityLogger.shared.level = .debug
  40. IQKeyboardManager.shared.enable = true
  41. if #available(iOS 10.0, *) {
  42. // For iOS 10 display notification (sent via APNS)
  43. UNUserNotificationCenter.current().delegate = self
  44. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  45. UNUserNotificationCenter.current().requestAuthorization(
  46. options: authOptions,
  47. completionHandler: {_, _ in })
  48. } else {
  49. let settings: UIUserNotificationSettings =
  50. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  51. application.registerUserNotificationSettings(settings)
  52. }
  53. application.registerForRemoteNotifications()
  54. FirebaseApp.configure()
  55. Fabric.with([Crashlytics.self])
  56. if UserDefaults.standard.object(forKey: "com.gmeremit.uuid") != nil
  57. {
  58. // print("Old UUID: \(savedUUID)")
  59. } else {
  60. let uuid = UUID().uuidString
  61. UserDefaults.standard.set(uuid, forKey: "com.gmeremit.uuid")
  62. // print("New UUID: \(uuid)")
  63. }
  64. if (UserDefaults.standard.object(forKey: "com.gmeremit.loginStatus")) != nil{
  65. redirectToApp()
  66. }
  67. return true
  68. }
  69. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  70. // If you are receiving a notification message while your app is in the background,
  71. // this callback will not be fired till the user taps on the notification launching the application.
  72. // TODO: Handle data of notification
  73. // With swizzling disabled you must let Messaging know about the message, for Analytics
  74. // Messaging.messaging().appDidReceiveMessage(userInfo)
  75. // Print message ID.
  76. if userInfo[gcmMessageIDKey] != nil {
  77. // print(" message: \(messageID)")
  78. }
  79. // Print full message.
  80. // print("fore ground ", userInfo)
  81. }
  82. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  83. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  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 ID completionHandler: \(messageID)")
  92. }
  93. // Print full message.
  94. // print(userInfo)
  95. /**
  96. redirection of page after background push notification
  97. */
  98. if
  99. let aps = userInfo["aps"] as? NSDictionary,
  100. let alert = aps["alert"] as? NSDictionary,
  101. let _ = alert["body"] as? String,
  102. let _ = alert["title"] as? String {
  103. // print("Title: \(title) \nBody:\(body)")
  104. let codeID = userInfo["code"] as? String
  105. // print("code", codeID!)
  106. self.redirectWithPushNotification(notificationCode: codeID!)
  107. }
  108. completionHandler(UIBackgroundFetchResult.newData)
  109. }
  110. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  111. // print("Firebase registration token: \(fcmToken)")
  112. cmRegistrationId = fcmToken
  113. notificationSet.value = true
  114. // TODO: If necessary send token to application server.
  115. // Note: This callback is fired at each app startup and whenever a new token is generated.
  116. }
  117. // [END refresh_token]
  118. // [START ios_10_data_message]
  119. // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
  120. // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
  121. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  122. // print("Received data message: \(remoteMessage.appData)")
  123. }
  124. public func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  125. }
  126. @available(iOS 10.0, *)
  127. func userNotificationCenter(_ center: UNUserNotificationCenter,
  128. willPresent notification: UNNotification,
  129. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  130. var userInfo = notification.request.content.userInfo
  131. // With swizzling disabled you must let Messaging know about the message, for Analytics
  132. // Messaging.messaging().appDidReceiveMessage(userInfo)
  133. // Print message ID.
  134. if userInfo[gcmMessageIDKey] != nil {
  135. // print("Message ID: def \(messageID)")
  136. }
  137. /**
  138. redirection of page after foreground push notification
  139. */
  140. if
  141. let aps = userInfo["aps"] as? NSDictionary,
  142. let alert = aps["alert"] as? NSDictionary,
  143. let body = alert["body"] as? String,
  144. let title = alert["title"] as? String {
  145. // print("Title: \(title) \nBody:\(body)")
  146. let codeID = userInfo["code"] as? String
  147. 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))
  148. banner.didTapBlock = {
  149. // print("Banner was tapped on \(Date())!")
  150. self.redirectWithPushNotification(notificationCode: codeID!)
  151. }
  152. banner.show(duration: 5.0)
  153. }
  154. if userInfo["code"] != nil {
  155. // print("codeAP delegate: \(codeID)")
  156. }
  157. // Change this to your preferred presentation option
  158. completionHandler([])
  159. }
  160. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  161. // print("Unable to register for remote notifications: \(error.localizedDescription)")
  162. }
  163. // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
  164. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
  165. // the FCM registration token.
  166. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  167. // print("APNs token retrieved: \(deviceToken)")
  168. // With swizzling disabled you must set the APNs token here.
  169. // Messaging.messaging().apnsToken = deviceToken
  170. }
  171. func applicationWillResignActive(_ application: UIApplication) {
  172. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  173. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  174. }
  175. func applicationDidEnterBackground(_ application: UIApplication) {
  176. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  177. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  178. }
  179. func applicationWillEnterForeground(_ application: UIApplication) {
  180. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  181. }
  182. func applicationDidBecomeActive(_ application: UIApplication) {
  183. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  184. }
  185. func applicationWillTerminate(_ application: UIApplication) {
  186. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  187. // print("App terminate")
  188. }
  189. /**
  190. redirection to home page if user is already login to app otherwise redirect to KYC form.
  191. */
  192. func redirectToApp() {
  193. let nav = UINavigationController()
  194. if (UserDefaults.standard.object(forKey: "com.gmeremit.loginStatus") as! Bool) {
  195. guard (UserDefaults.standard.object(forKey: UserKeys.kyc) as? Bool) ?? false == true else {
  196. let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
  197. if let KYCPersonalInfoViewController = storyboard.instantiateViewController(withIdentifier: "KYCPersonalInfoViewController") as? KYCPersonalInfoViewController {
  198. nav.viewControllers = [KYCPersonalInfoViewController]
  199. window?.rootViewController = nav
  200. }
  201. return
  202. }
  203. let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
  204. if let tabViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController {
  205. nav.viewControllers = [tabViewController]
  206. window?.rootViewController = nav
  207. }
  208. } else {
  209. let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
  210. if let splashScreenViewController = storyboard.instantiateViewController(withIdentifier: "SplashScreenViewController") as? SplashScreenViewController {
  211. nav.viewControllers = [splashScreenViewController]
  212. window?.rootViewController = nav
  213. }
  214. }
  215. }
  216. /**
  217. method for redirection of page when push notification tap
  218. */
  219. func redirectWithPushNotification(notificationCode: String){
  220. let nav = UINavigationController()
  221. switch notificationCode {
  222. case "500":
  223. InviteViewController.notificationCode = "p500"
  224. let storyboard = UIStoryboard(name: "Invite", bundle: nil)
  225. let viewController = storyboard.instantiateViewController(withIdentifier :"InviteViewController") as! InviteViewController
  226. let navController = UINavigationController.init(rootViewController: viewController)
  227. if let window = self.window, let rootViewController = window.rootViewController {
  228. var currentController = rootViewController
  229. while let presentedController = currentController.presentedViewController {
  230. currentController = presentedController
  231. }
  232. currentController.present(navController, animated: true, completion: nil)
  233. }
  234. case "200":
  235. TranscationStatementViewController.notificationCode = "p200"
  236. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  237. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  238. nav.viewControllers = [transcationStatementViewController]
  239. window?.rootViewController = nav
  240. }
  241. case "201":
  242. MoneyRequestViewController.notificationCode = "p201"
  243. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  244. if let moneyRequestViewController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as? MoneyRequestViewController {
  245. nav.viewControllers = [moneyRequestViewController]
  246. window?.rootViewController = nav
  247. }
  248. case "202":
  249. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "WalletTransactionListViewController")
  250. WalletTransactionListViewController.notificationCode = "p202"
  251. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  252. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  253. walletTransactionListViewController.walletStatus = "walletBorrow"
  254. nav.viewControllers = [walletTransactionListViewController]
  255. window?.rootViewController = nav
  256. }
  257. case "203":
  258. TranscationStatementViewController.notificationCode = "p203"
  259. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  260. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  261. nav.viewControllers = [transcationStatementViewController]
  262. window?.rootViewController = nav
  263. }
  264. case "204":
  265. TranscationStatementViewController.notificationCode = "p204"
  266. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  267. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  268. nav.viewControllers = [transcationStatementViewController]
  269. window?.rootViewController = nav
  270. }
  271. case "205":
  272. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "walletViewController")
  273. WalletTransactionListViewController.notificationCode = "p205"
  274. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  275. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  276. walletTransactionListViewController.walletStatus = "walletTransfer"
  277. nav.viewControllers = [walletTransactionListViewController]
  278. window?.rootViewController = nav
  279. }
  280. case "206":
  281. TranscationStatementViewController.notificationCode = "p200"
  282. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  283. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  284. nav.viewControllers = [transcationStatementViewController]
  285. window?.rootViewController = nav
  286. }
  287. case "207":
  288. TranscationStatementViewController.notificationCode = "p200"
  289. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  290. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  291. nav.viewControllers = [transcationStatementViewController]
  292. window?.rootViewController = nav
  293. }
  294. case "208":
  295. TranscationStatementViewController.notificationCode = "p208"
  296. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  297. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  298. nav.viewControllers = [transcationStatementViewController]
  299. window?.rootViewController = nav
  300. }
  301. case "700":
  302. // redirectViewController(storyBoardName: "Reward", identifier: "RewardViewController")
  303. RewardViewController.notificationCode = "p700"
  304. let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main)
  305. if let rewardViewController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as? RewardViewController {
  306. nav.viewControllers = [rewardViewController]
  307. window?.rootViewController = nav
  308. }
  309. case "750":
  310. return
  311. //redirectViewController(storyBoardName: "Home", identifier: "CommentsViewController")
  312. default:
  313. return
  314. }
  315. }
  316. }
  317. //extension AppDelegate : MessagingDelegate {
  318. // // [START refresh_token]
  319. // func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  320. // print("Firebase registration token: \(fcmToken)")
  321. //
  322. // // TODO: If necessary send token to application server.
  323. // // Note: This callback is fired at each app startup and whenever a new token is generated.
  324. // }
  325. // // [END refresh_token]
  326. // // [START ios_10_data_message]
  327. // // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
  328. // // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
  329. // func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  330. // print("Received data message: \(remoteMessage.appData)")
  331. // }
  332. // // [END ios_10_data_message]
  333. //}