我正在尝试使用远程推送通知更新实时活动。
我正在创建这样的现场活动:
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)
}
实时活动显示在通知中心,也可以收到pushToken。 现在我正在尝试使用远程通知更新实时活动。代码如下所示:
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) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
res.send("ok")
// return res.send("ok");
}).catch((error) => {
console.log(error)
return res.send(error.code);
});
});
发送远程通知有效,因为“警报”出现在我的手机上,但实时活动没有更新。有什么想法可以将接收到的“pushToken”插入到实时活动中吗?
你得到的 pushToken 应该在你发送请求的 URL 中。 例如,如果它是沙盒:https://api.sandbox.push.apple.com/3/device/insert-the-pushToken-here
这个 pushToken 曾经是 device token 但对于 Live Activity,只需将 device token 替换为 live activity 的 pushToken