

import Foundationimport ActivityKitstruct LiveActivityAttributes: ActivityAttributes {public struct ContentState: Codable, Hashable {// Dynamic stateful properties about your activity go here!var text: Stringvar pauseTime: Date?var endTime: Date?}// Fixed non-changing properties about your activity go here!// custom activityID when creating a LiveActivityvar activityID: String}
import ActivityKitimport WidgetKitimport SwiftUIstruct LiveActivityLiveActivity: Widget {var body: some WidgetConfiguration {ActivityConfiguration(for: LiveActivityAttributes.self) { context in// Lock screen/banner UI goes hereVStack {Text("Hello \\(context.state.text)")}.activityBackgroundTint(Color.cyan).activitySystemActionForegroundColor(Color.black)} dynamicIsland: { context inDynamicIsland {// Expanded UI goes here. Compose the expanded UI through// various regions, like leading/trailing/center/bottomDynamicIslandExpandedRegion(.leading) {Text("Leading \\(context.attributes.activityID)\\(context.state.text)")}DynamicIslandExpandedRegion(.trailing) {Text("Trailing \\(context.state.text)")}DynamicIslandExpandedRegion(.center) {Text("Center \\(context.state.text)")}DynamicIslandExpandedRegion(.bottom) {Text("Bottom \\(context.state.text)")// more content}} compactLeading: {Text("CL \\(context.state.text)")} compactTrailing: {Text("CT \\(context.state.text)")} minimal: {Text("CB \\(context.state.text)")}.widgetURL(URL(string: "https://www.tencentcloud.com/document/product/269/100621?from_cn_redirect=1")).keylineTint(Color.red)}}}

// startlet activity = try Activity.request(attributes: adventure,content: .init(state: initialState, staleDate: nil),pushType: .token)// updateawait activity.update(ActivityContent<AdventureAttributes.ContentState>(state: contentState,staleDate: Date.now + 15,relevanceScore: alert ? 100 : 50),alertConfiguration: alertConfig)// endawait activity.end(ActivityContent(state: finalContent, staleDate: nil), dismissalPolicy: dismissalPolicy)
Task {for await pushToken in activity.pushTokenUpdates {let pushTokenString = pushToken.hexadecimalStringLogger().debug("New push token: \\(pushTokenString)")try await self.setLiveActivity(activityID:activity.attributes.activityID, pushToken: pushToken)}}func setLiveActivity(activityID: String, pushToken: Data) async throws {var _apnsConfig = ImSDK_Plus.V2TIMLiveActivityConfig()_apnsConfig.businessID = xxxx_apnsConfig.token = pushToken_apnsConfig.activityID = activityIDos_log("%@", type: .debug, "setLiveActivity activityID: \\(activityID)\\ntoken:\\(pushToken.hexadecimalString)")ImSDK_Plus.V2TIMManager.sharedInstance().setLiveActivity(_apnsConfig, succ: {print("setLiveActivity succ")}, fail: {code, desc inprint("setLiveActivity fail, \\(code), \\(desc)")})}
func clearActivity(activityID: String) async throws {os_log("clearActivity ID: \\(activityID)")ImSDK_Plus.V2TIMManager.sharedInstance().setLiveActivity(nil, succ: {print("clearActivity succ")}, fail: {code, desc inprint("clearActivity fail, \\(code), \\(desc)")})}
Field Name | Type | Option | Field description |
LaId | string | Required | The flag of the real-time activity that needs to be pushed corresponds to the client activityID. The length does not exceed 64 bytes. |
Event | string | Required | update a policy Query an instance Reset the access password of an instance |
ContentState | JSON Object | Required | Custom key: value object. It needs to match the client SDK value. Corresponding apns official document: Starting and updating Live Activities with ActivityKit push notifications | Apple Developer Documentation. |
DismissalDate | Integer | Optional. | The uinx Time when the lock screen real-time activity ends if the event is end. If not filled in, it defaults to the current time, and the lock screen will end soon. |
{"MsgBody": [...] // Relevant description here for MsgBody"OfflinePushInfo": {"PushFlag": 0,"Title": "Offline Push Title""Desc": "Offline push content""Ext": "{\\"entity\\":{\\"k1\\":\\"v1\\",\\"k2\\":\\"v2\\"}}", // custom passthrough field, use string in json format for push"ApnsInfo": {"LiveActivity": {"LaId": "timpush","Event": "update", // update LA"ContentState": {"k1": v1,"k2": v2,...}}},"AndroidInfo": {... // Related reference of AndroidInfo in official website document}}}
{"MsgBody": [...] // Relevant description here for MsgBody"OfflinePushInfo": {"PushFlag": 0,"Title": "Offline Push Title""Desc": "Offline push content""Ext": "{\\"entity\\":{\\"k1\\":\\"v1\\",\\"k2\\":\\"v2\\"}}", // custom passthrough field, use string in json format for push"ApnsInfo": {"LiveActivity": {"LaId": "timpush","Event": "end", // end LA"ContentState": {"k1": v1,"k2": v2,...},"DismissalDate": 1739502750}},"AndroidInfo": {... // Related reference of AndroidInfo in official website document}}}
Feedback