-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
102 lines (89 loc) 路 3.46 KB
/
Copy pathAppDelegate.swift
File metadata and controls
102 lines (89 loc) 路 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Copyright 漏 2026 Stream.io Inc. All rights reserved.
//
import GDPerformanceView_Swift
import StreamVideo
import SwiftUI
import UIKit
class AppDelegate: NSObject, UIApplicationDelegate, @MainActor UNUserNotificationCenterDelegate {
@Injected(\.streamVideo) var streamVideo
@Injected(\.permissions) var permissions
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
UNUserNotificationCenter.current().delegate = self
setUpRemoteNotifications()
setUpPerformanceTracking()
// Setup a dummy video file to loop when working with from the simulator
InjectedValues[\.simulatorStreamFile] = Bundle.main.url(forResource: "test", withExtension: "mp4")
return true
}
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let deviceToken = deviceToken.map { String(format: "%02x", $0) }.joined()
AppState.shared.pushToken = deviceToken
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo = response.notification.request.content.userInfo
log.debug("push notification received \(userInfo)")
guard let stream = userInfo["stream"] as? [String: Any],
let callCid = stream["call_cid"] as? String else {
return
}
let components = callCid.components(separatedBy: ":")
if components.count >= 2 {
let callType = components[0]
let callId = components[1]
let call = streamVideo.call(callType: callType, callId: callId)
AppState.shared.activeCall = call
Task {
do {
try Task.checkCancellation()
try await streamVideo.connect()
try Task.checkCancellation()
try await call.accept()
try Task.checkCancellation()
try await call.join()
} catch {
log.error(error)
}
}
}
}
// MARK: - Private Helpers
private func setUpRemoteNotifications() {
Task {
do {
guard
try await permissions.requestPushNotificationPermission(with: [.alert, .sound, .badge])
else {
log.warning("Push notifications request not granted.")
return
}
_ = await Task { @MainActor in
UIApplication.shared.registerForRemoteNotifications()
}.result
} catch {
log.error("Push notifications request failed.", error: error)
}
}
}
private func setUpPerformanceTracking() {
guard AppEnvironment.performanceTrackerVisibility == .visible else { return }
// PerformanceMonitor seems to have a bug where it cannot find the
// hierarchy when trying to place its view.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
PerformanceMonitor.shared().performanceViewConfigurator.options = [
.performance
]
PerformanceMonitor.shared().start()
}
}
}