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.

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