我正在尝试使用远程推送通知更新实时活动。
我正在创建一个如下所示的实时活动:
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"吗?
1条答案
按热度按时间fcg9iug31#
你得到的pushToken应该在你发送请求的URL中。例如,如果它是到沙箱:https://api.sandbox.push.apple.com/3/device/insert-the-pushToken-here
此pushToken曾经是设备令牌,但对于实时活动,只需将设备令牌替换为实时活动的pushToken即可