借助 Firebase Cloud Messaging HTTP v1 API,您可以在 iOS 设备上远程发送、更新和结束实时活动通知。请注意,您需要 iOS 16.1 才能使用实时活动,需要 iOS 17.2 才能远程启动实时活动通知。
准备工作
开始在 Firebase Cloud Messaging 中使用实时活动之前,请按照设置 Firebase Cloud Messaging 客户端应用(Apple 平台)中的说明创建 Firebase Cloud Messaging 并将其添加到您的客户端应用中。
启动实时活动
如需使用 Firebase Cloud Messaging 远程启动实时活动,您需要从 Apple 获取推送启动令牌。您还需要用于目标应用的 FCM 注册令牌。
如需构建用于启动实时活动的载荷,请填写以下代码示例中的 apns.payload
字段,以使用 FCM 远程启动实时活动。
REST
{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TO_START_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "event": "start", "content-state": { "demo": 1 }, "attributes-type": "DemoAttributes", "attributes": { "demoAttribute": 1 }, "alert": { "title": "test title", "body": "test body" } } } } } }
cURL
curl -X POST -H "Authorization: Bearer OAUTH2_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TO_START_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "event": "start", "content-state": { "demo": 1 }, "attributes-type": "DemoAttributes", "attributes": { "demoAttribute": 1 }, "alert": { "title": "test title", "body": "test body" } } } } } }' https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send
点击运行,即可在 API Explorer 中试用该示例。
更新实时活动
如需使用 Firebase Cloud Messaging 远程更新实时活动,您需要从 Apple 获取推送令牌。您还需要用于目标应用的 FCM 注册令牌。
如需构建用于更新实时活动的载荷,请填写以下代码示例中的 apns.payload
字段,以使用 FCM 远程更新实时活动。
REST
{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "event": "update", "content-state": { "test1": 100, "test2": "demo" }, "alert": { "title": "test title", "body": "test body" } } } } } }
cURL
curl -X POST -H "Authorization: Bearer OAUTH2_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "event": "update", "content-state": { "test1": 100, "test2": "demo" }, "alert": { "title": "test title", "body": "test body" } } } } } }' https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send
点击运行,即可在 API Explorer 中试用该示例。
结束实时活动
如需使用 Firebase Cloud Messaging 结束实时活动,您需要从 Apple 获取推送令牌。您还需要用于目标应用的 FCM 注册令牌。
如需构建用于结束实时活动的载荷,请填写以下代码示例中的 apns.payload
字段,以使用 FCM 远程结束实时活动。
REST
{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "dismissal-date": DISMISSAL_DATE, "event": "end", "content-state": { "test1": 100, "test2": "demo" }, "alert": { "title": "test title", "body": "test body" } } } } } }
cURL
curl -X POST -H "Authorization: Bearer OAUTH2_ACCESS_TOKEN" -H "Content-Type: application/json" -d '{ "message": { "token": "FCM_TOKEN", "apns": { "live_activity_token": "LIVE_ACTIVITY_PUSH_TOKEN", "headers": { "apns-priority": "10" }, "payload": { "aps": { "timestamp": TIMESTAMP, "dismissal-date": DISMISSAL_DATE, "event": "end", "content-state": { "test1": 100, "test2": "demo" }, "alert": { "title": "test title", "body": "test body" } } } } } }' https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send
点击运行,即可在 API Explorer 中试用该示例。