Guest User

Untitled

a guest
Apr 18th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.38 KB | None | 0 0
  1. //
  2. //  AppDelegate.swift
  3. //  DholRainbow
  4. //
  5. //  Created by Patrik Spišák on 12/01/2022.
  6. //
  7.  
  8. import Foundation
  9. import Rainbow
  10. import SwiftUI
  11. import Intents
  12. import FirebaseCore
  13.  
  14. final class AppDelegate: NSObject, ObservableObject, UIApplicationDelegate {
  15.    
  16.     @Published var peerJID:         String  = ""
  17.     @Published var inPerson:        INPerson?
  18.     @Published var missedCalls:     Int     = 0
  19.    
  20.     @AppStorage("requestedPermissions") var requestedPermissions: Bool = false
  21.     @AppStorage("ringtone") var ringtone: String = "incoming-call.mp3"
  22.    
  23.     var shortcutItem: UIApplicationShortcutItem? { AppDelegate.shortcutItem }
  24.    
  25.     static var shortcutItem: UIApplicationShortcutItem?
  26.    
  27.     // Retrieved from https://developers.openrainbow.com/applications
  28.     //
  29.     private let appId:      String = Bundle.main.infoDictionary?["APP_ID"]      as? String ?? ""
  30.     private let appSecret:  String = Bundle.main.infoDictionary?["APP_SECRET"]  as? String ?? ""
  31.        
  32.     // By default you want all your views to rotate freely
  33.     static var orientationLock = UIInterfaceOrientationMask.portrait
  34.        
  35.     func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
  36.         return AppDelegate.orientationLock
  37.     }
  38.        
  39.     /// Add to new SwiftUI life cycle custom SceneDelegate
  40.     /// - Parameters:
  41.     ///   - application: `UIApplication`
  42.     ///   - connectingSceneSession: `UISceneSession`
  43.     ///   - options: `UIScene.ConnectionOptions`
  44.     /// - Returns: `UiSceneConfiguration`
  45.     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  46.                
  47.         if let shortcutItem = options.shortcutItem {
  48.             AppDelegate.shortcutItem = shortcutItem
  49.         }
  50.        
  51.         let sceneConfig = UISceneConfiguration(name: "Scene Configuration", sessionRole: connectingSceneSession.role)
  52.         sceneConfig.delegateClass = SceneDelegate.self
  53.         return sceneConfig
  54.     }
  55.  
  56.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  57.        
  58.         FirebaseApp.configure()
  59.        
  60.         // Notification Center
  61.         UNUserNotificationCenter.current().delegate = self
  62.        
  63.         // Override point for customization after application launch.
  64.         ServicesManager.sharedInstance()?.setAppID(appId, secretKey: appSecret)
  65.        
  66.         let outgoingRingTone = Bundle.main.path(forResource: "outgoing-rings", ofType: "mp3") ?? ""
  67.        
  68.         ServicesManager.sharedInstance().notificationsManager.useSandboxAPNs(false)
  69.        
  70.         // Register for push notifications
  71.         if requestedPermissions {
  72.             ServicesManager.sharedInstance().notificationsManager.registerForUserNotificationsSettings()
  73.         }
  74.    
  75.         ServicesManager.sharedInstance().rtcService.startCallKit(withIncomingSoundName: ringtone , iconTemplate: "voip-logo")
  76.        
  77.         ServicesManager.sharedInstance().rtcService.appSoundOutgoingCall = outgoingRingTone
  78.         ServicesManager.sharedInstance().rtcService.appSoundHangup = Bundle.main.path(forResource: "hangup", ofType: "wav") ?? ""
  79.        
  80.         return true
  81.     }
  82.    
  83.     func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  84.         completionHandler(UIBackgroundFetchResult.newData)
  85.     }
  86.    
  87.     /// Notificaiton when Rainbow server has changed
  88.     /// - Parameter notification: `NSNotification`
  89.     @objc private func didChangeServer(notification: NSNotification) {
  90.         if !Thread.isMainThread {
  91.             DispatchQueue.main.async {
  92.                 self.didChangeServer(notification: notification)
  93.             }
  94.             return
  95.         }
  96.     }
  97.    
  98.     func applicationWillTerminate(_ application: UIApplication) {
  99.        
  100.         // Hangup any active call if application is terminated
  101.         ServicesManager.sharedInstance().rtcService.hangupCallOnAppWillTerminate()
  102.                                
  103.         LogsRecorder.sharedInstance.stopRecord()
  104.        
  105.         // Clear all logs
  106.         LogsRecorder.sharedInstance.cleanOldLogs()
  107.     }
  108. }
  109.  
  110. extension AppDelegate: UNUserNotificationCenterDelegate {
  111.    
  112.     /// This function will be called when a notification arrives while the app is in the foreground
  113.     /// - Parameters:
  114.     ///   - center: `UNUserNotificationCenter`
  115.     ///   - notification: `UNNotification`
  116.     ///   - completionHandler: `UNNotificationPresentationOptions`
  117.     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  118.         let userInfo = notification.request.content.userInfo
  119.         ServicesManager.sharedInstance().notificationsManager.handleNotification(withIdentifier: userInfo.description, withUserInfo: userInfo, withResponseInformation: nil)
  120.        
  121.         completionHandler([.banner, .sound])
  122.     }
  123.        
  124.     /// This function will be called right after user tap on the notification
  125.     /// - Parameters:
  126.     ///   - center: `UNUserNotificationCenter`
  127.     ///   - response: `UNNotificationResponse`
  128.     ///   - completionHandler: `Void`
  129.     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  130.                
  131.         let userInfo = response.notification.request.content.userInfo
  132.                
  133.         ServicesManager.sharedInstance().notificationsManager.didReceiveNotification(userInfo: userInfo)
  134.        
  135.         if let _peerJID = userInfo["peerJID"] as? String {
  136.             self.peerJID = _peerJID
  137.         }
  138.        
  139.         if let _ = userInfo["missedCall"] as? Bool {
  140.             self.missedCalls += 1
  141.         }
  142.        
  143.         completionHandler()
  144.     }
  145.    
  146.     /// Ask user for push notifications permissions
  147.     /// - Parameters:
  148.     ///   - application: `UIApplication`
  149.     ///   - notificationSettings: `UNNotificationSettings`
  150.     func application(_ application: UIApplication, didRegister notificationSettings: UNNotificationSettings) {
  151.         // Request for push token
  152.         UIApplication.shared.registerForRemoteNotifications()
  153.     }
  154.    
  155.     /// User grant permissions for receiving push notifications
  156.     /// - Parameters:
  157.     ///   - app: `UIApplication`
  158.     ///   - deviceToken: `Data`
  159.     func application(_ app: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  160.         //NSLog("Did Register For Remote Notifications With Device Token : \(deviceToken.map({String(format: "%02x", $0)}).joined())")
  161.         ServicesManager.sharedInstance().notificationsManager.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
  162.     }
  163.    
  164.     /// User refuse grant permission for receiving push notifications
  165.     /// - Parameters:
  166.     ///   - app: `UIApplication`
  167.     ///   - error: `Error`
  168.     func application(_ app: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  169.         //NSLog("User refuse to enable push notification")
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment