-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOSLiveActivityViewExtensions.swift
More file actions
149 lines (132 loc) · 6.73 KB
/
Copy pathOSLiveActivityViewExtensions.swift
File metadata and controls
149 lines (132 loc) · 6.73 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Modified MIT License
Copyright 2025 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Effectively blanks out this file for Mac Catalyst
#if targetEnvironment(macCatalyst)
#else
import WidgetKit
import ActivityKit
import SwiftUI
@available(iOS 16.1, *)
extension DynamicIsland {
/// Sets the URL that opens the corresponding app of a Live Activity when a user taps on the Live Activity.
/// Sets OneSignal activity metadata. See Important callout below on usage.
///
/// By setting the URL with this function, it becomes the default URL for deep linking into the app
/// for each view of the Live Activity. However, if you include a
/// <doc://com.apple.documentation/documentation/swiftui/link> in the Live Activity,
/// the link takes priority over the default URL. When a person taps on the `Link`, it takes them to the
/// place in the app that corresponds to the URL of the `Link`.
///
/// - Parameters:
/// - url: The URL that opens the app.
/// - context: The activity view context.
///
/// - Returns: The configuration object for the Dynamic Island with the specified URL.
///
/// > Important: Use instead of`.widgetURL`. Requires handling from your app's URL handling code
/// (e.g., `application(_:open:options:)` in AppDelegate or `onOpenURL` in SwiftUI) using the
/// `OneSignal.LiveActivities.trackClickAndReturnOriginal(url)` method.
public func onesignalWidgetURL<T: OneSignalLiveActivityAttributes>(
_ url: URL?,
context: ActivityViewContext<T>
) -> DynamicIsland {
return self.widgetURL(LiveActivityTrackingUtils.generateTrackingDeepLink(originalURL: url, context: context))
}
}
@available(iOS 16.1, *)
extension View {
/// Sets the URL to open in the containing app when the user clicks the widget.
/// Sets OneSignal activity metadata. See Important callout below on usage.
///
/// - Parameters:
/// - url: The URL to open in the containing app.
/// - context: The activity view context.
/// - Returns: A view that opens the specified URL when the user clicks
/// the widget.
///
/// Widgets support one `onesignalWidgetURL` modifier in their view hierarchy.
/// If multiple views have `onesignalWidgetURL` modifiers, the behavior is undefined.
///
/// > Important: Use instead of`.widgetURL`. Requires handling from your app's URL handling code
/// (e.g., `application(_:open:options:)` in AppDelegate or `onOpenURL` in SwiftUI) using the
/// `OneSignal.LiveActivities.trackClickAndReturnOriginal(url)` method.
@MainActor @preconcurrency public func onesignalWidgetURL<T: OneSignalLiveActivityAttributes>(_ url: URL?, context: ActivityViewContext<T>) -> some View {
return self.widgetURL(LiveActivityTrackingUtils.generateTrackingDeepLink(originalURL: url, context: context))
}
}
// MARK: - Tracking Utilities
/// Utilities for building and managing Live Activity tracking URLs
enum LiveActivityTrackingUtils {
/// Generates a tracking deep link from an original URL and activity context
/// - Parameters:
/// - originalURL: The original URL to track clicks for
/// - context: The activity view context containing metadata
/// - Returns: The tracking URL, or nil if construction failed
@available(iOS 16.1, *)
static func generateTrackingDeepLink<T: OneSignalLiveActivityAttributes>(originalURL: URL?, context: ActivityViewContext<T>) -> URL? {
// Get activity metadata from context
let activityId = context.attributes.onesignal.activityId
let activityType = String(describing: T.self)
let notificationId = context.state.onesignal?.notificationId
return buildTrackingURL(
originalURL: originalURL,
activityId: activityId,
activityType: activityType,
notificationId: notificationId
)
}
/// Builds a tracking URL that wraps the original URL with OneSignal tracking parameters
/// - Parameters:
/// - originalURL: The original URL to track clicks for
/// - activityId: The activity identifier
/// - activityType: The activity type name
/// - notificationId: Optional notification ID
/// - Returns: The tracking URL, or nil if construction failed
static func buildTrackingURL(
originalURL: URL?,
activityId: String,
activityType: String,
notificationId: String?
) -> URL? {
// Generate a unique click ID
let clickId = UUID().uuidString
// Build OneSignal tracking URL
var components = URLComponents()
components.scheme = LiveActivityConstants.Tracking.scheme
components.host = LiveActivityConstants.Tracking.host
components.path = LiveActivityConstants.Tracking.clickPath
var queryItems = [
URLQueryItem(name: LiveActivityConstants.Tracking.clickId, value: clickId),
URLQueryItem(name: LiveActivityConstants.Tracking.activityId, value: activityId),
URLQueryItem(name: LiveActivityConstants.Tracking.activityType, value: activityType),
URLQueryItem(name: LiveActivityConstants.Tracking.notificationId, value: notificationId)
]
if let originalURL = originalURL {
// URLQueryItem automatically percent-encodes the value when URLComponents constructs the URL
// This ensures special characters like &, #, ?, etc. in the redirect URL are properly encoded
queryItems.append(URLQueryItem(name: LiveActivityConstants.Tracking.redirect, value: originalURL.absoluteString))
}
components.queryItems = queryItems
return components.url
}
}
#endif