AppDelegate.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // AppDelegate.swift
  3. // TestQmeraLite
  4. //
  5. // Created by Qindi on 29/11/21.
  6. //
  7. import UIKit
  8. import NexilisLite
  9. import NotificationBannerSwift
  10. import StreamShield
  11. import PushKit
  12. import FirebaseCore
  13. @main
  14. class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate {
  15. var appName = Bundle.main.infoDictionary?["CFBundleName"] as! String
  16. var apikey = "***REPLACE***WITH***YOUR***ACCOUNT***"
  17. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  18. var showButton = false
  19. if PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_FLOATING || PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_MIX {
  20. showButton = true
  21. }
  22. Nexilis.isShowForceSignIn = false
  23. APIS.connect(appName: appName , apiKey: apikey, delegate: self, showButton: showButton, fromMAB: true)
  24. registerForPushNotifications()
  25. registerForVoIPPushNotifications()
  26. FirebaseApp.configure()
  27. APIS.monitoredActivity()
  28. return true
  29. }
  30. // MARK: UISceneSession Lifecycle
  31. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  32. // Called when a new scene session is being created.
  33. // Use this method to select a configuration to create the new scene with.
  34. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  35. }
  36. func applicationWillTerminate(_ application: UIApplication) {
  37. APIS.willTerminate()
  38. APIS.enterBackground()
  39. }
  40. private func registerForPushNotifications() {
  41. if #available(iOS 10.0, *) {
  42. let center = UNUserNotificationCenter.current()
  43. center.delegate = self
  44. center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
  45. if error == nil && granted {
  46. DispatchQueue.main.async {
  47. UIApplication.shared.registerForRemoteNotifications()
  48. }
  49. }
  50. }
  51. }
  52. else {
  53. UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
  54. UIApplication.shared.registerForRemoteNotifications()
  55. }
  56. }
  57. func registerForVoIPPushNotifications() {
  58. let mainQueue = DispatchQueue.main
  59. let voipRegistry = PKPushRegistry(queue: mainQueue)
  60. voipRegistry.delegate = self
  61. voipRegistry.desiredPushTypes = [.voIP]
  62. }
  63. func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
  64. let deviceToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
  65. APIS.sendPushToken(deviceToken, isCall: true)
  66. }
  67. func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
  68. let payload = payload.dictionaryPayload
  69. APIS.showNotificationCallKitNexilis(payload: payload) {
  70. completion()
  71. }
  72. }
  73. }
  74. extension AppDelegate: ConnectDelegate, UNUserNotificationCenterDelegate {
  75. func onSuccess(userId: String) {
  76. // print(#function, "userId: \(userId)")
  77. SecurityShield.check(appName: appName, apiKey: apikey)
  78. }
  79. func onFailed(error: String) {
  80. //print(#function, "error: \(error)")
  81. }
  82. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  83. let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
  84. APIS.sendPushToken(deviceTokenString)
  85. }
  86. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  87. //print(error)
  88. }
  89. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  90. APIS.showNotificationNexilis(userInfo, completionHandler)
  91. }
  92. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  93. completionHandler([.banner, .sound, .badge])
  94. }
  95. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  96. APIS.openNotificationNexilis(response)
  97. completionHandler()
  98. }
  99. }