Skip to content

Commit

Permalink
* Change convenience method from monitor to setup and add setup o…
Browse files Browse the repository at this point in the history
…ptions for more granular control.

* Do not retry LA reqeust when retryable failure until app restarts
  • Loading branch information
brismithers committed Feb 27, 2024
1 parent cce2051 commit a95cdea
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import Foundation
import ActivityKit
import UserNotifications
import OneSignalUser
import OneSignalLiveActivities
import OneSignalFramework

@objc
Expand All @@ -40,8 +38,8 @@ class LiveActivityController: NSObject {
static func start() {
// ExampleAppFirstWidgetAttributes and ExampleAppSecondWidgetAttributes enable the OneSignal SDK to
// listen for start/update tokens, this is the only call needed.
OneSignal.LiveActivities.monitor(ExampleAppFirstWidgetAttributes.self)
OneSignal.LiveActivities.monitor(ExampleAppSecondWidgetAttributes.self)
OneSignal.LiveActivities.setup(ExampleAppFirstWidgetAttributes.self)
OneSignal.LiveActivities.setup(ExampleAppSecondWidgetAttributes.self)

if #available(iOS 17.2, *) {
// ExampleAppThirdWidgetAttributes is an example of how to manually set up LA.
Expand Down
17 changes: 17 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
4746E2A72B86B64100D6324C /* LiveActivitiesSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4746E2A62B86B64100D6324C /* LiveActivitiesSwiftTests.swift */; };
4746E2AB2B8775C400D6324C /* LiveActivitiesObjcTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4746E2AA2B8775C400D6324C /* LiveActivitiesObjcTests.m */; };
4746E2AD2B8833C200D6324C /* OSLiveActivities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4746E2AC2B8833C200D6324C /* OSLiveActivities.swift */; };
4746E2AE2B8946F200D6324C /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
4746E2AF2B8946F200D6324C /* OneSignalCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
47AE653C2B7D39DF006EBAA9 /* OneSignalLiveActivities.h in Headers */ = {isa = PBXBuildFile; fileRef = 47AE653B2B7D39DF006EBAA9 /* OneSignalLiveActivities.h */; settings = {ATTRIBUTES = (Public, ); }; };
47AE653F2B7D39DF006EBAA9 /* OneSignalLiveActivities.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 47AE65392B7D39DF006EBAA9 /* OneSignalLiveActivities.framework */; };
47AE65402B7D39DF006EBAA9 /* OneSignalLiveActivities.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 47AE65392B7D39DF006EBAA9 /* OneSignalLiveActivities.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -569,6 +571,13 @@
remoteGlobalIDString = DE69E19A282ED8060090BB3D;
remoteInfo = OneSignalUser;
};
4746E2B02B8946F200D6324C /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
proxyType = 1;
remoteGlobalIDString = DE7D17E527026B95002D3A5D;
remoteInfo = OneSignalCore;
};
47AE653D2B7D39DF006EBAA9 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
Expand Down Expand Up @@ -802,6 +811,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
4746E2AF2B8946F200D6324C /* OneSignalCore.framework in Embed Frameworks */,
472620BA2B7D6EF90014013A /* OneSignalUser.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
Expand Down Expand Up @@ -1293,6 +1303,7 @@
buildActionMask = 2147483647;
files = (
472620B92B7D6EF90014013A /* OneSignalUser.framework in Frameworks */,
4746E2AE2B8946F200D6324C /* OneSignalCore.framework in Frameworks */,
472620B82B7D6E6D0014013A /* ActivityKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2616,6 +2627,7 @@
);
dependencies = (
472620BC2B7D6EFA0014013A /* PBXTargetDependency */,
4746E2B12B8946F200D6324C /* PBXTargetDependency */,
);
name = OneSignalLiveActivities;
productName = OneSignalLiveActivities;
Expand Down Expand Up @@ -3467,6 +3479,11 @@
target = DE69E19A282ED8060090BB3D /* OneSignalUser */;
targetProxy = 472620BB2B7D6EFA0014013A /* PBXContainerItemProxy */;
};
4746E2B12B8946F200D6324C /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DE7D17E527026B95002D3A5D /* OneSignalCore */;
targetProxy = 4746E2B02B8946F200D6324C /* PBXContainerItemProxy */;
};
47AE653E2B7D39DF006EBAA9 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 47AE65382B7D39DF006EBAA9 /* OneSignalLiveActivities */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class OSLiveActivitiesExecutor : OSPushSubscriptionObserver {

// The live activities request dispatch queue, serial. This synchronizes access to `updateTokens` and `startTokens`.
private var requestDispatch: DispatchQueue = DispatchQueue(label: "OneSignal.LiveActivities")
private var pollIntervalSeconds = 60
private var pollIntervalSeconds = 30

func start() {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities starting executor")
Expand Down Expand Up @@ -216,7 +216,7 @@ class OSLiveActivitiesExecutor : OSPushSubscriptionObserver {
return
}
}
self.pollPendingRequests()
// retryable failures will stay in the cache, and will retry the next time the app starts
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ public protocol OSLiveActivities {

public extension OSLiveActivities {
/**
Enable the OneSignalSDK to monitor the provided`ActivityAttributes` structure, which conforms to the
Enable the OneSignalSDK to setup the provided`ActivityAttributes` structure, which conforms to the
`OneSignalLiveActivityAttributes`. When using this function, OneSignal will manage the capturing
and synchronizing of both pushToStart and pushToUpdate tokens.
- Parameters
- activityType: The specific `OneSignalLiveActivityAttributes` structure tied to the live activity.
- options: An optional structure to provide for more granular setup options.
*/
@available(iOS 16.1, *)
static func monitor<T : OneSignalLiveActivityAttributes>(_ activityType: T.Type) {
OneSignalLiveActivitiesManagerImpl.monitor(activityType)
static func setup<T : OneSignalLiveActivityAttributes>(_ activityType: T.Type, options: LiveActivitySetupOptions? = nil) {
OneSignalLiveActivitiesManagerImpl.setup(activityType, options: options)
}

/**
Expand All @@ -127,3 +128,19 @@ public extension OSLiveActivities {
OneSignalLiveActivitiesManagerImpl.removePushToStartToken(activityType)
}
}

/**
The setup options for `OneSignal.LiveActivities.setup`.
*/
public struct LiveActivitySetupOptions {
/**
When true, OneSignal will listen for pushToStart tokens for the `OneSignalLiveActivityAttributes` structure.
*/
public var enablePushToStart: Bool = true

/**
When true, OneSignal will listen for pushToUpdate tokens for each start live activity that uses the
`OneSignalLiveActivityAttributes` structure.
*/
public var enablePushToUpdate: Bool = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
}

@available(iOS 16.1, *)
public static func monitor<Attributes : OneSignalLiveActivityAttributes>(_ activityType: Attributes.Type) {
if #available(iOS 17.2, *) {
public static func setup<Attributes : OneSignalLiveActivityAttributes>(_ activityType: Attributes.Type, options: LiveActivitySetupOptions? = nil) {
if #available(iOS 17.2, *), (options == nil || options!.enablePushToStart) {
Task {
for try await data in Activity<Attributes>.pushToStartTokenUpdates {
let token = data.map {String(format: "%02x", $0)}.joined()
Expand Down Expand Up @@ -134,11 +134,13 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
}
}

// listen for activity update token updates so we can tell OneSignal how to update the activity
Task {
for await pushToken in activity.pushTokenUpdates {
let token = pushToken.map {String(format: "%02x", $0)}.joined()
OneSignalLiveActivitiesManagerImpl.enter(activity.attributes.onesignal.activityId, withToken: token)
if (options == nil || options!.enablePushToUpdate) {
// listen for activity update token updates so we can tell OneSignal how to update the activity
Task {
for await pushToken in activity.pushTokenUpdates {
let token = pushToken.map {String(format: "%02x", $0)}.joined()
OneSignalLiveActivitiesManagerImpl.enter(activity.attributes.onesignal.activityId, withToken: token)
}
}
}
}
Expand Down

0 comments on commit a95cdea

Please sign in to comment.