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. APIS.connect(appName: appName , apiKey: apikey, delegate: self, showButton: showButton, fromMAB: true)
  23. registerForPushNotifications()
  24. registerForVoIPPushNotifications()
  25. FirebaseApp.configure()
  26. APIS.monitoredActivity()
  27. return true
  28. }
  29. // MARK: UISceneSession Lifecycle
  30. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  31. // Called when a new scene session is being created.
  32. // Use this method to select a configuration to create the new scene with.
  33. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  34. }
  35. func applicationWillTerminate(_ application: UIApplication) {
  36. APIS.willTerminate()
  37. APIS.enterBackground()
  38. }
  39. private func registerForPushNotifications() {
  40. if #available(iOS 10.0, *) {
  41. let center = UNUserNotificationCenter.current()
  42. center.delegate = self
  43. center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
  44. if error == nil && granted {
  45. DispatchQueue.main.async {
  46. UIApplication.shared.registerForRemoteNotifications()
  47. }
  48. }
  49. }
  50. }
  51. else {
  52. UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
  53. UIApplication.shared.registerForRemoteNotifications()
  54. }
  55. }
  56. func registerForVoIPPushNotifications() {
  57. let mainQueue = DispatchQueue.main
  58. let voipRegistry = PKPushRegistry(queue: mainQueue)
  59. voipRegistry.delegate = self
  60. voipRegistry.desiredPushTypes = [.voIP]
  61. }
  62. func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
  63. let deviceToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
  64. APIS.sendPushToken(deviceToken, isCall: true)
  65. }
  66. func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
  67. let payload = payload.dictionaryPayload
  68. APIS.showNotificationCallKitNexilis(payload: payload) {
  69. completion()
  70. }
  71. }
  72. }
  73. extension AppDelegate: ConnectDelegate, UNUserNotificationCenterDelegate {
  74. func onSuccess(userId: String) {
  75. // print(#function, "userId: \(userId)")
  76. SecurityShield.check(appName: appName, apiKey: apikey)
  77. }
  78. func onFailed(error: String) {
  79. //print(#function, "error: \(error)")
  80. }
  81. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  82. let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
  83. APIS.sendPushToken(deviceTokenString)
  84. }
  85. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  86. //print(error)
  87. }
  88. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  89. APIS.showNotificationNexilis(userInfo)
  90. completionHandler(.newData)
  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. }