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.

182 lines
5.1 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 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 Localize_Swift
  20. import ChannelIO
  21. #if DEBUG
  22. var server: Server = .live
  23. #else
  24. let server: Server = .live
  25. #endif
  26. var overlayView: UIView?
  27. @UIApplicationMain
  28. class AppDelegate: UIResponder, UIApplicationDelegate {
  29. var window: UIWindow?
  30. func application(
  31. _ application: UIApplication,
  32. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  33. ) -> Bool {
  34. UIApplication.shared.applicationIconBadgeNumber = 0
  35. #if DEBUG
  36. NetworkActivityLogger.shared.startLogging()
  37. NetworkActivityLogger.shared.level = .debug
  38. #endif
  39. IQKeyboardManager.shared.enable = true
  40. IQKeyboardManager.shared.shouldResignOnTouchOutside = true
  41. FirebaseApp.configure()
  42. Fabric.with([Crashlytics.self])
  43. Fabric.sharedSDK().debug = true
  44. Messaging.messaging().delegate = self
  45. let center = UNUserNotificationCenter.current()
  46. center.delegate = self
  47. center.requestAuthorization(options: [.alert, .sound, .badge]) { (_, _) in}
  48. application.registerForRemoteNotifications()
  49. // Initialize ChannelIO
  50. ChannelIO.initialize()
  51. self.window?.rootViewController = LauncherScreenWireframe().getMainView()
  52. GMEDB
  53. .shared
  54. .app
  55. .remove([.isOpenedTokenRenwalAlert, .isOpenedPopupNotification])
  56. StoreReviewHelper.shared.incrementAppOpenedCount()
  57. return true
  58. }
  59. func application(
  60. _ application: UIApplication,
  61. didReceiveRemoteNotification userInfo: [AnyHashable: Any]
  62. ) { }
  63. func application(
  64. _ application: UIApplication,
  65. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  66. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  67. ) {
  68. ChannelIO.handlePushNotification(userInfo) {
  69. completionHandler(.noData)
  70. }
  71. }
  72. func application(
  73. _ application: UIApplication,
  74. didFailToRegisterForRemoteNotificationsWithError error: Error
  75. ) { }
  76. func application(
  77. _ application: UIApplication,
  78. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  79. ) {
  80. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  81. print("APNs device token: \(deviceTokenString)")
  82. Messaging.messaging().apnsToken = deviceToken
  83. ChannelIO.initPushToken(deviceToken: deviceToken)
  84. }
  85. func applicationDidEnterBackground(_ application: UIApplication) {
  86. let language = Localize.currentLanguage()
  87. GMEDB.shared.app.set(language, .currentLanguage)
  88. let viewcontroller = UIViewController()
  89. viewcontroller.view.backgroundColor = .themeRed
  90. viewcontroller.view.frame = UIScreen.main.bounds
  91. overlayView = viewcontroller.view
  92. guard let view = overlayView else {return}
  93. let topView = UIApplication.shared.keyWindow?.subviews.last
  94. topView?.addSubview(view)
  95. topView?.bringSubviewToFront(view)
  96. }
  97. func applicationWillEnterForeground(_ application: UIApplication) {
  98. if let language = GMEDB.shared.app.string(.currentLanguage) {
  99. Localize.setCurrentLanguage(language)
  100. }
  101. overlayView?.removeFromSuperview()
  102. }
  103. }
  104. // MARK: - UNUserNotificationCenterDelegate
  105. extension AppDelegate: UNUserNotificationCenterDelegate {
  106. func userNotificationCenter(
  107. _ center: UNUserNotificationCenter,
  108. willPresent notification: UNNotification,
  109. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  110. ) {
  111. let userInfo = notification.request.content.userInfo
  112. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  113. completionHandler([
  114. UNNotificationPresentationOptions.alert,
  115. UNNotificationPresentationOptions.sound,
  116. UNNotificationPresentationOptions.badge
  117. ])
  118. }
  119. func userNotificationCenter(
  120. _ center: UNUserNotificationCenter,
  121. didReceive response: UNNotificationResponse,
  122. withCompletionHandler completionHandler: @escaping () -> Void
  123. ) {
  124. let userInfo = response.notification.request.content.userInfo
  125. print("Message ID: \(userInfo["gcm_message_id"] ?? "nil")")
  126. if ChannelIO.isChannelPushNotification(userInfo) {
  127. ChannelIO.handlePushNotification(userInfo)
  128. }
  129. print(userInfo)
  130. completionHandler()
  131. }
  132. }
  133. // MARK: - MessagingDelegate
  134. extension AppDelegate: MessagingDelegate {
  135. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  136. print("Firebase registration token: \(fcmToken)")
  137. let userInfo = ["token": fcmToken]
  138. NotificationCenter.default.post(
  139. name: Notification.Name("FCMToken"),
  140. object: nil,
  141. userInfo: userInfo
  142. )
  143. }
  144. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  145. print("Received data message: \(remoteMessage.appData)")
  146. }
  147. }