|
26 | 26 | */
|
27 | 27 |
|
28 | 28 | import Foundation
|
29 |
| -import ActivityKit |
30 | 29 | import UserNotifications
|
| 30 | +import OneSignalFramework |
| 31 | +#if targetEnvironment(macCatalyst) |
| 32 | +#else |
| 33 | +import ActivityKit |
| 34 | +import OneSignalLiveActivities |
| 35 | +@objc |
| 36 | +class LiveActivityController: NSObject { |
| 37 | + |
| 38 | + @available(iOS 16.1, *) |
| 39 | + @objc |
| 40 | + static func start() { |
| 41 | + // ExampleAppFirstWidgetAttributes and ExampleAppSecondWidgetAttributes enable the OneSignal SDK to |
| 42 | + // listen for start/update tokens, this is the only call needed. |
| 43 | + OneSignal.LiveActivities.setup(ExampleAppFirstWidgetAttributes.self) |
| 44 | + OneSignal.LiveActivities.setup(ExampleAppSecondWidgetAttributes.self) |
31 | 45 |
|
32 |
| -struct OneSignalWidgetAttributes: ActivityAttributes { |
33 |
| - public struct ContentState: Codable, Hashable { |
34 |
| - // Dynamic stateful properties about your activity go here! |
35 |
| - var message: String |
| 46 | + // There is a "built in" Live Activity Widget Attributes called `DefaultLiveActivityAttributes`. |
| 47 | + // This is mostly for cross-platform SDKs and allows OneSignal to handle everything but the |
| 48 | + // creation of the Widget Extension. |
| 49 | + OneSignal.LiveActivities.setupDefault() |
| 50 | + |
| 51 | + if #available(iOS 17.2, *) { |
| 52 | + // ExampleAppThirdWidgetAttributes is an example of how to manually set up LA. |
| 53 | + // Setup an async task to monitor and send pushToStartToken updates to OneSignalSDK. |
| 54 | + Task { |
| 55 | + for try await data in Activity<ExampleAppThirdWidgetAttributes>.pushToStartTokenUpdates { |
| 56 | + let token = data.map {String(format: "%02x", $0)}.joined() |
| 57 | + OneSignal.LiveActivities.setPushToStartToken(ExampleAppThirdWidgetAttributes.self, withToken: token) |
| 58 | + } |
| 59 | + } |
| 60 | + // Setup an async task to monitor for an activity to be started, for each started activity we |
| 61 | + // can then set up an async task to monitor and send updateToken updates to OneSignalSDK. We |
| 62 | + // filter out LA started in-app, because the `createActivity` function below does its own |
| 63 | + // updateToken update monitoring. If there can be multiple instances of this activity-type, |
| 64 | + // the activity-id (i.e. "my-activity-id") is most likely passed down as an attribute within |
| 65 | + // ExampleAppThirdWidgetAttributes. |
| 66 | + Task { |
| 67 | + for await activity in Activity<ExampleAppThirdWidgetAttributes>.activityUpdates |
| 68 | + where activity.attributes.isPushToStart { |
| 69 | + Task { |
| 70 | + for await pushToken in activity.pushTokenUpdates { |
| 71 | + let token = pushToken.map {String(format: "%02x", $0)}.joined() |
| 72 | + OneSignal.LiveActivities.enter("my-activity-id", withToken: token) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
36 | 78 | }
|
37 | 79 |
|
38 |
| - // Fixed non-changing properties about your activity go here! |
39 |
| - var title: String |
40 |
| -} |
41 |
| -@objc |
42 |
| -class LiveActivityController: NSObject { |
43 |
| - // To aid in testing |
44 |
| - static var counter = 0 |
| 80 | + /** |
| 81 | + An example of starting a Live Activity whose attributes are "OneSignal SDK aware". The SDK will handle listening for update tokens on behalf of the app. |
| 82 | + */ |
| 83 | + static var counter1 = 0 |
| 84 | + @available(iOS 13.0, *) |
| 85 | + @objc |
| 86 | + static func createOneSignalAwareActivity(activityId: String) { |
| 87 | + if #available(iOS 16.1, *) { |
| 88 | + counter1 += 1 |
| 89 | + let oneSignalAttribute = OneSignalLiveActivityAttributeData.create(activityId: activityId) |
| 90 | + let attributes = ExampleAppFirstWidgetAttributes(title: "#" + String(counter1) + " OneSignal Dev App Live Activity", onesignal: oneSignalAttribute) |
| 91 | + let contentState = ExampleAppFirstWidgetAttributes.ContentState(message: "Update this message through push or with Activity Kit") |
| 92 | + do { |
| 93 | + _ = try Activity<ExampleAppFirstWidgetAttributes>.request( |
| 94 | + attributes: attributes, |
| 95 | + contentState: contentState, |
| 96 | + pushType: .token) |
| 97 | + } catch let error { |
| 98 | + print(error.localizedDescription) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + An example of starting a Live Activity using the DefaultLiveActivityAttributes. The SDK will handle listening for update tokens on behalf of the app. |
| 105 | + */ |
| 106 | + @available(iOS 13.0, *) |
| 107 | + @objc |
| 108 | + static func createDefaultActivity(activityId: String) { |
| 109 | + if #available(iOS 16.1, *) { |
| 110 | + let attributeData: [String: Any] = ["title": "in-app-title"] |
| 111 | + let contentData: [String: Any] = ["message": ["en": "HELLO", "es": "HOLA"], "progress": 0.58, "status": "1/15", "bugs": 2] |
| 112 | + |
| 113 | + OneSignal.LiveActivities.startDefault(activityId, attributes: attributeData, content: contentData) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + An example of starting a Live Activity whose attributes are **not** "OneSignal SDK aware". The app must handle listening for update tokens and notify the OneSignal SDK. |
| 119 | + */ |
| 120 | + static var counter2 = 0 |
45 | 121 | @available(iOS 13.0, *)
|
46 | 122 | @objc
|
47 |
| - static func createActivity() async -> String? { |
| 123 | + static func createActivity(activityId: String) async { |
48 | 124 | if #available(iOS 16.1, *) {
|
49 |
| - counter += 1 |
50 |
| - let attributes = OneSignalWidgetAttributes(title: "#" + String(counter) + " OneSignal Dev App Live Activity") |
51 |
| - let contentState = OneSignalWidgetAttributes.ContentState(message: "Update this message through push or with Activity Kit") |
| 125 | + counter2 += 1 |
| 126 | + let attributes = ExampleAppThirdWidgetAttributes(title: "#" + String(counter2) + " OneSignal Dev App Live Activity", isPushToStart: false) |
| 127 | + let contentState = ExampleAppThirdWidgetAttributes.ContentState(message: "Update this message through push or with Activity Kit") |
52 | 128 | do {
|
53 |
| - let activity = try Activity<OneSignalWidgetAttributes>.request( |
| 129 | + let activity = try Activity<ExampleAppThirdWidgetAttributes>.request( |
54 | 130 | attributes: attributes,
|
55 | 131 | contentState: contentState,
|
56 | 132 | pushType: .token)
|
57 | 133 | for await data in activity.pushTokenUpdates {
|
58 | 134 | let myToken = data.map {String(format: "%02x", $0)}.joined()
|
59 |
| - return myToken |
| 135 | + OneSignal.LiveActivities.enter(activityId, withToken: myToken) |
60 | 136 | }
|
61 | 137 | } catch let error {
|
62 | 138 | print(error.localizedDescription)
|
63 |
| - return nil |
64 | 139 | }
|
65 | 140 | }
|
66 |
| - return nil |
67 | 141 | }
|
68 | 142 | }
|
| 143 | +#endif |
0 commit comments