You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Live Activities更新推送与点击事件追踪问题求助

解决iOS Live Activities的两个追踪问题

1. 追踪基于Token的推送更新事件

场景1:应用处于前台/后台

在UNUserNotificationCenterDelegate的回调中识别Live Activity更新推送并埋点:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // 判断是否为Live Activity更新推送(无alert字段,包含content-state字段)
    if userInfo["aps"]?["alert"] == nil && userInfo["aps"]?["content-state"] != nil {
        // 这里添加你的埋点逻辑,比如上报Live Activity更新事件
        print("Live Activity 收到推送更新")
    }
    completionHandler()
}

场景2:应用被完全杀死

Live Activity的更新会在独立进程中处理,可在LiveActivity的didReceive(_:)方法中记录事件,再通过App Groups同步到主App:

struct YourActivityAttributes: ActivityAttributes {
    // 自定义你的属性字段
}

extension YourActivityAttributes {
    struct ContentState: Codable, Hashable {
        // 自定义状态属性
    }
}

class YourLiveActivity: UIActivity {
    override func didReceive(_ update: ActivityUpdate<YourActivityAttributes>) {
        super.didReceive(update)
        // 将更新事件存入App Groups共享容器
        guard let sharedDefaults = UserDefaults(suiteName: "your.app.group.id") else { return }
        sharedDefaults.set(Date(), forKey: "LastLiveActivityUpdateTimestamp")
        sharedDefaults.synchronize()
    }
}

用户下次打开App时,读取App Groups中的数据,同步到你的分析系统即可。

2. 追踪锁屏Live Activity的点击操作

步骤1:配置Live Activity的点击跳转URL

创建Live Activity时,为ActivityConfiguration指定点击跳转的自定义URL:

let activityAttributes = YourActivityAttributes()
let initialContentState = YourActivityAttributes.ContentState(/* 初始化状态参数 */)
do {
    let activity = try Activity.request(
        attributes: activityAttributes,
        contentState: initialContentState,
        pushType: .token
    )
    // 配置点击跳转URL,携带Activity ID用于区分
    let configuration = ActivityConfiguration(for: activityAttributes) { context in
        // Live Activity静态界面配置
    } dynamicIsland: { context in
        // Dynamic Island界面配置
    }
    configuration.url = URL(string: "yourapp://liveactivity?activityId=\(activity.id)")
} catch {
    print("请求Live Activity失败: \(error)")
}

步骤2:在App中捕获跳转事件

用SceneDelegate的情况:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else { return }
    if url.scheme == "yourapp" && url.host == "liveactivity" {
        let activityId = url.queryParameters?["activityId"] ?? ""
        // 添加埋点逻辑,记录Live Activity点击事件
        print("用户点击Live Activity,Activity ID: \(activityId)")
    }
}

// 扩展URL,方便解析query参数
extension URL {
    var queryParameters: [String: String]? {
        guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true),
              let queryItems = components.queryItems else { return nil }
        return queryItems.reduce(into: [String: String]()) { result, item in
            result[item.name] = item.value
        }
    }
}

用AppDelegate的情况:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.scheme == "yourapp" && url.host == "liveactivity" {
        let activityId = url.queryParameters?["activityId"] ?? ""
        // 埋点逻辑
        print("用户点击Live Activity,Activity ID: \(activityId)")
        return true
    }
    return false
}

如果未自定义URL,也可通过NSUserActivity判断:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = connectionOptions.userActivities.first,
       userActivity.activityType == "com.apple.activitykit.activity" {
        // 记录Live Activity点击启动事件
        print("用户通过点击Live Activity启动App")
    }
}

内容的提问来源于stack exchange,提问作者Abhishek Ranjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:25:27