Skip to content
Open
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
10 changes: 10 additions & 0 deletions iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ @implementation AppDelegate

OneSignalNotificationCenterDelegate *_notificationDelegate;

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
// Log the full tracking URL and the original extracted URL
// Also trigger trackClickAndReturnOriginal twice to confirm this click event is only sent once
NSLog(@"Dev App: application openURL FULL URL is %@", url);
NSURL *originalURL1 = [OneSignal.LiveActivities trackClickAndReturnOriginal:url];
NSURL *originalURL2 = [OneSignal.LiveActivities trackClickAndReturnOriginal:url];
NSLog(@"Dev App: application openURL processed, original URL is %@", originalURL1);
return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// [FIRApp configure];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ import OneSignalLiveActivities
}
Spacer()
}
.onesignalWidgetURL(URL(string: "myapp://product/12345"), context: context)
// .widgetURL(URL(string: "myapp://product/12345"))
.activitySystemActionForegroundColor(.black)
.activityBackgroundTint(.white)
} dynamicIsland: { _ in
} dynamicIsland: { context in
DynamicIsland {
// Expanded UI goes here. Compose the expanded UI through
// various regions, like leading/trailing/center/bottom
Expand All @@ -75,7 +77,8 @@ import OneSignalLiveActivities
} minimal: {
Text("Min")
}
.widgetURL(URL(string: "http://www.apple.com"))
.onesignalWidgetURL(URL(string: "myapp://product/12345"), context: context)
// .widgetURL(URL(string: "myapp://product/12345"))
.keylineTint(Color.red)
}
}
Expand Down
12 changes: 12 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@
3C14E39F2AFAE39B006ED053 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C14E39E2AFAE39B006ED053 /* PrivacyInfo.xcprivacy */; };
3C14E3A12AFAE461006ED053 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */; };
3C14E3A42AFAE54C006ED053 /* OneSignalSwiftInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC08AFF2947D4E900C81DA3 /* OneSignalSwiftInterface.swift */; };
3C19C6322E919F0C00D6731E /* OSRequestLiveActivityClicked.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C19C6312E919F0C00D6731E /* OSRequestLiveActivityClicked.swift */; };
3C24B0EC2BD09D7A0052E771 /* OneSignalCoreObjCTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C24B0EB2BD09D7A0052E771 /* OneSignalCoreObjCTests.m */; };
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */; };
3C2C7DC8288F3C020020F9AE /* OSSubscriptionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */; };
3C2D8A5928B4C4E300BE41F6 /* OSDelta.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */; };
3C2DB2F12DE6CB5E0006B905 /* OneSignalBadgeHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C2DB2EF2DE6CB5E0006B905 /* OneSignalBadgeHelpers.h */; settings = {ATTRIBUTES = (Public, ); }; };
3C2DB2F22DE6CB5E0006B905 /* OneSignalBadgeHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C2DB2F02DE6CB5E0006B905 /* OneSignalBadgeHelpers.m */; };
3C3D34E92E95EAA5006A2924 /* LiveActivityConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3D34E82E95EAA5006A2924 /* LiveActivityConstants.swift */; };
3C3D8D782E92DB7500C3E977 /* OSLiveActivityViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3D8D772E92DB7500C3E977 /* OSLiveActivityViewExtensions.swift */; };
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
3C44673F296D09CC0039A49E /* OneSignalMobileProvision.h in Headers */ = {isa = PBXBuildFile; fileRef = 912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */; settings = {ATTRIBUTES = (Public, ); }; };
3C448B9D2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C448B9B2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h */; };
Expand Down Expand Up @@ -1251,6 +1254,7 @@
3C11518C289AF5E800565C41 /* OSModelChangedHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSModelChangedHandler.swift; sourceTree = "<group>"; };
3C14E39E2AFAE39B006ED053 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
3C19C6312E919F0C00D6731E /* OSRequestLiveActivityClicked.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSRequestLiveActivityClicked.swift; sourceTree = "<group>"; };
3C24B0EA2BD09D790052E771 /* OneSignalCoreTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "OneSignalCoreTests-Bridging-Header.h"; sourceTree = "<group>"; };
3C24B0EB2BD09D7A0052E771 /* OneSignalCoreObjCTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalCoreObjCTests.m; sourceTree = "<group>"; };
3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModelRepo.swift; sourceTree = "<group>"; };
Expand All @@ -1259,6 +1263,8 @@
3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSDelta.swift; sourceTree = "<group>"; };
3C2DB2EF2DE6CB5E0006B905 /* OneSignalBadgeHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalBadgeHelpers.h; sourceTree = "<group>"; };
3C2DB2F02DE6CB5E0006B905 /* OneSignalBadgeHelpers.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalBadgeHelpers.m; sourceTree = "<group>"; };
3C3D34E82E95EAA5006A2924 /* LiveActivityConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveActivityConstants.swift; sourceTree = "<group>"; };
3C3D8D772E92DB7500C3E977 /* OSLiveActivityViewExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLiveActivityViewExtensions.swift; sourceTree = "<group>"; };
3C448B9B2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSBackgroundTaskHandlerImpl.h; sourceTree = "<group>"; };
3C448B9C2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSBackgroundTaskHandlerImpl.m; sourceTree = "<group>"; };
3C448BA12936B474002F96BC /* OSBackgroundTaskManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSBackgroundTaskManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2258,6 +2264,7 @@
3CFA8F472E9087DB00201FE5 /* OSRequestSetUpdateToken.swift */,
3CFA8F452E9087DB00201FE5 /* OSRequestRemoveUpdateToken.swift */,
3CFA8F5A2E9091A200201FE5 /* OSRequestLiveActivityReceiveReceipts.swift */,
3C19C6312E919F0C00D6731E /* OSRequestLiveActivityClicked.swift */,
);
path = Requests;
sourceTree = "<group>";
Expand All @@ -2269,8 +2276,10 @@
3CFA8F482E9087DB00201FE5 /* Requests */,
3CFA8F4B2E9087DB00201FE5 /* OneSignalLiveActivitiesManagerImpl.swift */,
3CFA8F4C2E9087DB00201FE5 /* OneSignalLiveActivityAttributes.swift */,
3C3D8D772E92DB7500C3E977 /* OSLiveActivityViewExtensions.swift */,
3CFA8F4D2E9087DB00201FE5 /* OSLiveActivitiesExtension.swift */,
3CFA8F492E9087DB00201FE5 /* AnyCodable.swift */,
3C3D34E82E95EAA5006A2924 /* LiveActivityConstants.swift */,
3CFA8F4A2E9087DB00201FE5 /* DefaultLiveActivityAttributes.swift */,
);
path = Source;
Expand Down Expand Up @@ -4304,13 +4313,16 @@
buildActionMask = 2147483647;
files = (
3CFA8F4F2E9087DB00201FE5 /* AnyCodable.swift in Sources */,
3C3D8D782E92DB7500C3E977 /* OSLiveActivityViewExtensions.swift in Sources */,
3C3D34E92E95EAA5006A2924 /* LiveActivityConstants.swift in Sources */,
3CFA8F502E9087DB00201FE5 /* OSLiveActivitiesExecutor.swift in Sources */,
3CFA8F512E9087DB00201FE5 /* DefaultLiveActivityAttributes.swift in Sources */,
3CFA8F522E9087DB00201FE5 /* OSRequestSetStartToken.swift in Sources */,
3CFA8F532E9087DB00201FE5 /* OSRequestRemoveStartToken.swift in Sources */,
3CFA8F542E9087DB00201FE5 /* OSLiveActivitiesExtension.swift in Sources */,
3CFA8F552E9087DB00201FE5 /* OneSignalLiveActivitiesManagerImpl.swift in Sources */,
3CFA8F562E9087DB00201FE5 /* OSRequestSetUpdateToken.swift in Sources */,
3C19C6322E919F0C00D6731E /* OSRequestLiveActivityClicked.swift in Sources */,
3CFA8F572E9087DB00201FE5 /* OSRequestRemoveUpdateToken.swift in Sources */,
3CFA8F582E9087DB00201FE5 /* OSLiveActivityRequest.swift in Sources */,
3CFA8F592E9087DB00201FE5 /* OneSignalLiveActivityAttributes.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,6 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH} HTTP
#define OS_LIVE_ACTIVITIES_EXECUTOR_UPDATE_TOKENS_KEY @"OS_LIVE_ACTIVITIES_EXECUTOR_UPDATE_TOKENS_KEY"
#define OS_LIVE_ACTIVITIES_EXECUTOR_START_TOKENS_KEY @"OS_LIVE_ACTIVITIES_EXECUTOR_START_TOKENS_KEY"
#define OS_LIVE_ACTIVITIES_EXECUTOR_RECEIVE_RECEIPTS_KEY @"OS_LIVE_ACTIVITIES_EXECUTOR_RECEIVE_RECEIPTS_KEY"
#define OS_LIVE_ACTIVITIES_EXECUTOR_CLICKED_KEY @"OS_LIVE_ACTIVITIES_EXECUTOR_CLICKED_KEY"

#endif /* OneSignalCommonDefines_h */
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,21 @@ class ReceiveReceiptsRequestCache: RequestCache {
}
}

class ClickedRequestCache: RequestCache {
// Keep click event requests for up to 30 days.
static let OneMonthInSeconds = TimeInterval(60 * 60 * 24 * 30)

init() {
super.init(cacheKey: OS_LIVE_ACTIVITIES_EXECUTOR_CLICKED_KEY, ttl: ClickedRequestCache.OneMonthInSeconds)
}
}

class OSLiveActivitiesExecutor: OSPushSubscriptionObserver {
// The currently tracked update and start tokens (key) and their associated request (value). THESE ARE NOT THREAD SAFE
let updateTokens: UpdateRequestCache = UpdateRequestCache()
let startTokens: StartRequestCache = StartRequestCache()
let receiveReceipts: ReceiveReceiptsRequestCache = ReceiveReceiptsRequestCache()
let clickEvents: ClickedRequestCache = ClickedRequestCache()

// The live activities request dispatch queue, serial. This synchronizes access to `updateTokens` and `startTokens`.
private var requestDispatch: OSDispatchQueue
Expand Down Expand Up @@ -193,13 +203,16 @@ class OSLiveActivitiesExecutor: OSPushSubscriptionObserver {
block(self.startTokens)
block(self.updateTokens)
block(self.receiveReceipts)
block(self.clickEvents)
}

private func getCache(_ request: OSLiveActivityRequest) -> RequestCache {
if request is OSLiveActivityUpdateTokenRequest {
return self.updateTokens
} else if request is OSLiveActivityStartTokenRequest {
return self.startTokens
} else if request is OSRequestLiveActivityClicked {
return self.clickEvents
}

return self.receiveReceipts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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.
*/

/// Constants used throughout the OneSignalLiveActivities module
enum LiveActivityConstants {
/// URL components for OneSignal click tracking
enum Tracking {
static let scheme = "onesignal-liveactivity"
static let host = "track"
static let clickPath = "/click"
static let clickId = "clickId"
static let activityId = "activityId"
static let activityType = "activityType"
static let redirect = "redirect"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
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.
*/

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(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(generateTrackingDeepLink(originalURL: url, context: context))
}
}

// MARK: - Helper Function

@available(iOS 16.1, *)
private func generateTrackingDeepLink<T: OneSignalLiveActivityAttributes>(originalURL: URL?, context: ActivityViewContext<T>) -> URL? {
// Generate a unique click ID
let clickId = UUID().uuidString

// Get activity metadata
let activityId = context.attributes.onesignal.activityId
let activityType = String(describing: T.self)

// 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)
]

if let originalURL = originalURL {
queryItems.append(URLQueryItem(name: LiveActivityConstants.Tracking.redirect, value: originalURL.absoluteString))
}

components.queryItems = queryItems

return components.url
}
Loading
Loading