AppDelegate.swift 3.9 KB

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