Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing]Allow push notification sending/handling #575

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 58 additions & 46 deletions DemoApp/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import UIKit
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {

@Injected(\.streamVideo) var streamVideo
@Injected(\.pushNotificationAdapter) var pushNotificationAdapter

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
UNUserNotificationCenter.current().delegate = self
setUpRemoteNotifications()
_ = pushNotificationAdapter
setUpPerformanceTracking()

// Setup a dummy video file to loop when working with from the simulator
Expand Down Expand Up @@ -58,53 +58,65 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
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)
}
}
}
}
//
// func userNotificationCenter(
// _ center: UNUserNotificationCenter,
// didReceive response: UNNotificationResponse
// ) async {
// do {
// try await pushNotificationAdapter.handleNotification(response)
// } catch {
// log.error(error)
// }
//// 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
//// 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)
//// }
//// }
// }
//
// func userNotificationCenter(
// _ center: UNUserNotificationCenter,
// willPresent notification: UNNotification
// ) async -> UNNotificationPresentationOptions {
// log.debug("Will present received push notification: \(notification).")
// return [.banner, .sound]
// }

// MARK: - Private Helpers

private func setUpRemoteNotifications() {
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
// private func setUpRemoteNotifications() {
// UNUserNotificationCenter
// .current()
// .requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in
// if granted {
// Task { @MainActor in
// UIApplication.shared.registerForRemoteNotifications()
// }
// }
// }
// }

private func setUpPerformanceTracking() {
guard AppEnvironment.performanceTrackerVisibility == .visible else { return }
Expand Down
43 changes: 40 additions & 3 deletions DemoApp/Sources/Components/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,30 @@ extension AppEnvironment {
}
}

func joinLink(_ callId: String, callType: String = .default) -> URL {
var identifier: String {
switch self {
case .pronto:
return "pronto"
case .prontoStaging:
return "pronto-staging"
case .staging:
return "staging"
case .legacy:
return "legacy"
case .demo:
return "demo"
case let .custom:
return "custom"
}
}

func joinLink(
_ callId: String,
callType: String = .default,
apiKey: String? = nil,
userId: String? = nil,
token: String? = nil
) -> URL {
switch self {
case .demo:
return url
Expand All @@ -85,17 +108,26 @@ extension AppEnvironment {
.appendingPathComponent("join")
.appendingPathComponent(callId)
.addQueryParameter("type", value: callType)
.addQueryParameter("api_key", value: apiKey)
.addQueryParameter("user_id", value: userId)
.addQueryParameter("token", value: token)
case let .custom(baseURL, _, _):
return baseURL
.url
.appendingPathComponent("join")
.appendingPathComponent(callId)
.addQueryParameter("type", value: callType)
.addQueryParameter("api_key", value: apiKey)
.addQueryParameter("user_id", value: userId)
.addQueryParameter("token", value: token)
default:
return url
.appendingPathComponent("join")
.appendingPathComponent(callId)
.addQueryParameter("type", value: callType)
.addQueryParameter("api_key", value: apiKey)
.addQueryParameter("user_id", value: userId)
.addQueryParameter("token", value: token)
}
}

Expand Down Expand Up @@ -252,6 +284,7 @@ extension AppEnvironment {

enum SupportedDeeplink: Debuggable, CaseIterable {
case pronto
case prontoStaging
case staging
case demo
case legacy
Expand All @@ -260,6 +293,8 @@ extension AppEnvironment {
switch self {
case .pronto:
return BaseURL.pronto.url
case .prontoStaging:
return BaseURL.prontoStaging.url
case .staging:
return BaseURL.staging.url
case .demo:
Expand All @@ -273,6 +308,8 @@ extension AppEnvironment {
switch self {
case .pronto:
return "Pronto"
case .prontoStaging:
return "Pronto Staging"
case .staging:
return "Staging"
case .demo:
Expand All @@ -286,9 +323,9 @@ extension AppEnvironment {
static var supportedDeeplinks: [SupportedDeeplink] = {
switch configuration {
case .debug:
return [.pronto, .demo, .staging, .legacy]
return [.pronto, .prontoStaging, .demo, .staging, .legacy]
case .test:
return [.pronto, .demo, .staging, .legacy]
return [.pronto, .prontoStaging, .demo, .staging, .legacy]
case .release:
return [.demo]
}
Expand Down
22 changes: 18 additions & 4 deletions DemoApp/Sources/Components/Deeplinks/DeeplinkAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ struct DeeplinkInfo: Equatable {
var callId: String
var callType: String
var baseURL: AppEnvironment.BaseURL
var apiKey: String?
var token: String?
var userId: String?

static let empty = DeeplinkInfo(
url: nil,
callId: "",
callType: "",
baseURL: AppEnvironment.baseURL
baseURL: AppEnvironment.baseURL,
apiKey: nil,
token: nil,
userId: nil
)
}

Expand All @@ -25,15 +31,20 @@ struct DeeplinkAdapter {
return true
}

let result = AppEnvironment
let supported = AppEnvironment
.supportedDeeplinks
.compactMap(\.deeplinkURL.host)

let result = supported
.first { url.host == $0 } != nil

return result
}

func handle(url: URL) -> (deeplinkInfo: DeeplinkInfo, user: User?) {
func handle(url: URL) -> (
deeplinkInfo: DeeplinkInfo,
user: User?
) {
guard canHandle(url: url) else {
return (.empty, nil)
}
Expand Down Expand Up @@ -79,7 +90,10 @@ struct DeeplinkAdapter {
url: url,
callId: callId,
callType: callType,
baseURL: baseURL
baseURL: baseURL,
apiKey: url.queryParameters["api_key"],
token: url.queryParameters["token"],
userId: url.queryParameters["user_id"]
),
nil
)
Expand Down
Loading
Loading