Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppDelegate.swift
- // DholRainbow
- //
- // Created by Patrik Spišák on 12/01/2022.
- //
- import Foundation
- import Rainbow
- import SwiftUI
- import Intents
- import FirebaseCore
- final class AppDelegate: NSObject, ObservableObject, UIApplicationDelegate {
- @Published var peerJID: String = ""
- @Published var inPerson: INPerson?
- @Published var missedCalls: Int = 0
- @AppStorage("requestedPermissions") var requestedPermissions: Bool = false
- @AppStorage("ringtone") var ringtone: String = "incoming-call.mp3"
- var shortcutItem: UIApplicationShortcutItem? { AppDelegate.shortcutItem }
- static var shortcutItem: UIApplicationShortcutItem?
- // Retrieved from https://developers.openrainbow.com/applications
- //
- private let appId: String = Bundle.main.infoDictionary?["APP_ID"] as? String ?? ""
- private let appSecret: String = Bundle.main.infoDictionary?["APP_SECRET"] as? String ?? ""
- // By default you want all your views to rotate freely
- static var orientationLock = UIInterfaceOrientationMask.portrait
- func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
- return AppDelegate.orientationLock
- }
- /// Add to new SwiftUI life cycle custom SceneDelegate
- /// - Parameters:
- /// - application: `UIApplication`
- /// - connectingSceneSession: `UISceneSession`
- /// - options: `UIScene.ConnectionOptions`
- /// - Returns: `UiSceneConfiguration`
- func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
- if let shortcutItem = options.shortcutItem {
- AppDelegate.shortcutItem = shortcutItem
- }
- let sceneConfig = UISceneConfiguration(name: "Scene Configuration", sessionRole: connectingSceneSession.role)
- sceneConfig.delegateClass = SceneDelegate.self
- return sceneConfig
- }
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
- FirebaseApp.configure()
- // Notification Center
- UNUserNotificationCenter.current().delegate = self
- // Override point for customization after application launch.
- ServicesManager.sharedInstance()?.setAppID(appId, secretKey: appSecret)
- let outgoingRingTone = Bundle.main.path(forResource: "outgoing-rings", ofType: "mp3") ?? ""
- ServicesManager.sharedInstance().notificationsManager.useSandboxAPNs(false)
- // Register for push notifications
- if requestedPermissions {
- ServicesManager.sharedInstance().notificationsManager.registerForUserNotificationsSettings()
- }
- ServicesManager.sharedInstance().rtcService.startCallKit(withIncomingSoundName: ringtone , iconTemplate: "voip-logo")
- ServicesManager.sharedInstance().rtcService.appSoundOutgoingCall = outgoingRingTone
- ServicesManager.sharedInstance().rtcService.appSoundHangup = Bundle.main.path(forResource: "hangup", ofType: "wav") ?? ""
- return true
- }
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- completionHandler(UIBackgroundFetchResult.newData)
- }
- /// Notificaiton when Rainbow server has changed
- /// - Parameter notification: `NSNotification`
- @objc private func didChangeServer(notification: NSNotification) {
- if !Thread.isMainThread {
- DispatchQueue.main.async {
- self.didChangeServer(notification: notification)
- }
- return
- }
- }
- func applicationWillTerminate(_ application: UIApplication) {
- // Hangup any active call if application is terminated
- ServicesManager.sharedInstance().rtcService.hangupCallOnAppWillTerminate()
- LogsRecorder.sharedInstance.stopRecord()
- // Clear all logs
- LogsRecorder.sharedInstance.cleanOldLogs()
- }
- }
- extension AppDelegate: UNUserNotificationCenterDelegate {
- /// This function will be called when a notification arrives while the app is in the foreground
- /// - Parameters:
- /// - center: `UNUserNotificationCenter`
- /// - notification: `UNNotification`
- /// - completionHandler: `UNNotificationPresentationOptions`
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let userInfo = notification.request.content.userInfo
- ServicesManager.sharedInstance().notificationsManager.handleNotification(withIdentifier: userInfo.description, withUserInfo: userInfo, withResponseInformation: nil)
- completionHandler([.banner, .sound])
- }
- /// This function will be called right after user tap on the notification
- /// - Parameters:
- /// - center: `UNUserNotificationCenter`
- /// - response: `UNNotificationResponse`
- /// - completionHandler: `Void`
- func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
- let userInfo = response.notification.request.content.userInfo
- ServicesManager.sharedInstance().notificationsManager.didReceiveNotification(userInfo: userInfo)
- if let _peerJID = userInfo["peerJID"] as? String {
- self.peerJID = _peerJID
- }
- if let _ = userInfo["missedCall"] as? Bool {
- self.missedCalls += 1
- }
- completionHandler()
- }
- /// Ask user for push notifications permissions
- /// - Parameters:
- /// - application: `UIApplication`
- /// - notificationSettings: `UNNotificationSettings`
- func application(_ application: UIApplication, didRegister notificationSettings: UNNotificationSettings) {
- // Request for push token
- UIApplication.shared.registerForRemoteNotifications()
- }
- /// User grant permissions for receiving push notifications
- /// - Parameters:
- /// - app: `UIApplication`
- /// - deviceToken: `Data`
- func application(_ app: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- //NSLog("Did Register For Remote Notifications With Device Token : \(deviceToken.map({String(format: "%02x", $0)}).joined())")
- ServicesManager.sharedInstance().notificationsManager.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
- }
- /// User refuse grant permission for receiving push notifications
- /// - Parameters:
- /// - app: `UIApplication`
- /// - error: `Error`
- func application(_ app: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
- //NSLog("User refuse to enable push notification")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment