

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 |
Event | String | Required | Push event type for Live Activity: fill in start to start, update to update, and end to end. For an introduction to Live Activity in APNs, refer to the APNs documentation. |
AttributesType | String | Optional | Required when Event is "start". The name of the Live Activity attributes type, which must exactly match the name of the attributes type struct defined on the client. |
Attributes | JSON Object | Optional | Required when Event is "start". A custom key:value object representing the static attributes declared in the Live Activity attributes type, used to initialize the Live Activity. Must match the values of the client SDK. |
LaId | String | Optional | Required when Event is "update" or "end". The Live Activity identifier, corresponding to the client activityID, with a length not exceeding 64 bytes. |
ContentState | JSON Object | Required | Required when Event is "start", "update", or "end". A custom key:value object representing the dynamic attributes declared in the Live Activity attributes type. Must match the values of the client SDK. |
DismissalDate | Integer | Optional | When Event is "end", the Unix time at which the Live Activity ends its display on the lock screen. Defaults to the current time, dismissing the lock screen activity immediately. |
{// other parameters..."OfflinePushInfo": {"Title": "Offline Push Title","Desc": "Offline push content","Ext": "{\\"entity\\":{\\"k1\\":\\"v1\\",\\"k2\\":\\"v2\\"}}", // passthrough field, push uses string in json format"ApnsInfo": {"LiveActivity": {"LaId": "timpush","Event": "update", // Update LiveActivity push"ContentState": {"k1": "v1","k2": "v2",...}}},"AndroidInfo": {... // For details, see AndroidInfo field description}}}
{// other parameters..."OfflinePushInfo": {"Title": "Offline Push Title","Desc": "Offline push content","Ext": "{\\"entity\\":{\\"k1\\":\\"v1\\",\\"k2\\":\\"v2\\"}}", // passthrough field, push uses string in json format"ApnsInfo": {"LiveActivity": {"LaId": "timpush","Event": "end", // End LiveActivity push"ContentState": {"k1": "v1","k2": "v2",...},"DismissalDate": 1739502750}},"AndroidInfo": {... // For details, see AndroidInfo field description}}}
ActivityConfiguration(for: XXXAttributes.self) is implemented for the desired type in the WidgetExtension.pushToStartTokenUpdates async sequence, similar to handling update tokens. Report the token via V2TIMLiveActivityConfig. The key difference is: when reporting, specify attributesType (the activity type name); there is no need to provide activityID.// ActivityAttributes type for remote start listeningstruct LiveActivityAttributes: ActivityAttributes {public struct ContentState: Codable, Hashable {var k1: String?var k2: String?}var activityID: Stringvar k1: String?var k2: String?}// Remote start requires iOS 17.2 or aboveif #available(iOS 17.2, *) {Task {for await tokenData in Activity<LiveActivityAttributes>.pushToStartTokenUpdates {Logger().debug("New push-to-start token: \\(tokenData.hexadecimalString)")try await self.setStartLiveActivity(attributesType: "LiveActivityAttributes", pushToken: tokenData)}}}@available(iOS 17.2, *)func setStartLiveActivity(attributesType: String, pushToken: Data) async throws {let config = ImSDK_Plus.V2TIMLiveActivityConfig()config.businessID = xxxx // Replace with the P8 certificate ID uploaded in the Chat consoleconfig.token = pushToken// Remote start scenario: set attributesType (must exactly match the ActivityAttributes class name on the client), activityID is not requiredconfig.attributesType = attributesTypeImSDK_Plus.V2TIMManager.sharedInstance().setLiveActivity(config, succ: {print("setStartLiveActivity succ")}, fail: { code, desc inprint("setStartLiveActivity fail, \\(code), \\(desc)")})}
pushToStartTokenUpdates separately and report the corresponding attributesType for each.attributes-type field must exactly match the ActivityAttributes class name on the client. If the system cannot find a matching type, the request will be discarded. attributes and content-state fields must be decodable by their corresponding client-side ActivityAttributes and ContentState types. Missing any required (non-optional) fields will cause the entire start operation to fail. The start event must include an alert.{// other parameters..."OfflinePushInfo": {"Title": "Offline Push Title","Desc": "Offline push content","Ext": "{\\"entity\\":{\\"k1\\":\\"v1\\",\\"k2\\":\\"v2\\"}}", // passthrough field, push uses string in json format"ApnsInfo": {"LiveActivity": {"Event": "start", // start LiveActivity"AttributesType": "AdventureAttrs","Attributes":{"k1": "v1","k2": "k2",...},"ContentState": {"k1": "v1","k2": "v2",...}}},"AndroidInfo": {... // For details, see AndroidInfo field description}}
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan