AppDelegate.swift 4.8 KB

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