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

SwiftUI中通过远程推送通知更新Live Activity的问题

问题:无法通过远程推送更新Live Activity

我正在尝试通过远程推送通知更新Live Activity。目前Live Activity已在通知中心显示,且能成功获取到pushToken,但发送远程推送后,手机能收到alert通知,Live Activity却并未更新。

我的iOS端创建Live Activity代码

do{
    let activity = try Activity<LiveMatchesAttributes>.request(attributes: attributes, contentState: state, pushType: .token)

    print("Activity Added Successfully. id: \(activity.id)")
        
    Task {
       for await data in activity.pushTokenUpdates {
          let myToken = data.map {String(format: "%02x", $0)}.joined()

           print("pushToken", myToken)
           
       }
    }

}catch{
    print(error.localizedDescription)
}

我的后端(Firebase Cloud Functions)推送代码

exports.updateLiveActivities = functions.https.onRequest((req, res) => {
const fcmToken = "fcmToken"

const apns = {
    headers: {
        "apns_push_type" : "liveactivity",
        "apns_topic" : "bundle_id.push-type.liveactivity",
    },

    "payload": {
        "aps": {
        "timestamp" : Date.now(),
        "event": "update",
        "content-state": {
            "event": "start"
          },
        "alert": {
            "title": "Race Update",
            "body": "Tony Stark is now leading the race!"
         }
        },

    }

}

const message = {
    token: fcmToken,
    apns: apns
}

admin.messaging().send(message).then((response) => {
    console.log('Successfully sent message:', response);
    res.send("ok")
}).catch((error) => {
    console.log(error)
    return res.send(error.code);
});
});

解决方案

核心问题是你混淆了FCM设备令牌和Live Activity专属Push Token,正确的操作方式如下:

  • 将iOS端从activity.pushTokenUpdates获取到的16进制pushToken(即你打印的myToken),替换后端代码里const fcmToken = "fcmToken"这一行的字符串值。
  • Live Activity的pushToken是每个活动实例独有的,不是设备全局的FCM令牌,你需要把这个token上传到后端存储(比如数据库),后续要更新哪个Live Activity,就用对应的token发送推送。
  • 检查apns_topic是否正确:必须替换成你的App实际Bundle ID,格式为你的Bundle ID.push-type.liveactivity。
  • 去掉aps里的alert字段:这个字段会触发普通弹窗通知,更新Live Activity不需要它,只保留timestamp、event和content-state即可,这样推送只会更新Live Activity,不会弹出额外通知。

修改后的后端代码示例:

exports.updateLiveActivities = functions.https.onRequest((req, res) => {
// 替换成iOS端获取到的Live Activity专属pushToken
const liveActivityToken = "从iOS端获取的16进制pushToken值"

const apns = {
    headers: {
        "apns_push_type" : "liveactivity",
        // 替换成你的实际Bundle ID
        "apns_topic" : "com.yourapp.bundleid.push-type.liveactivity",
    },

    "payload": {
        "aps": {
        "timestamp" : Date.now(),
        "event": "update",
        "content-state": {
            "event": "start"
          }
        }
    }

}

const message = {
    token: liveActivityToken,
    apns: apns
}

admin.messaging().send(message).then((response) => {
    console.log('Successfully sent message:', response);
    res.send("ok")
}).catch((error) => {
    console.log(error)
    return res.send(error.code);
});
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 00:52:40