透過 Firebase Cloud Messaging HTTP v1 API,您可以在 iOS 裝置上遠端傳送、更新及結束即時活動通知。請注意,您必須使用 iOS 16.1 才能使用即時動態,並使用 iOS 17.2 才能遠端啟動即時動態通知。
事前準備
開始在 Firebase Cloud Messaging 上使用即時活動前,請按照「在 Apple 平台上設定 Firebase 雲端通訊用戶端應用程式」中的操作說明,建立並將 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註冊權杖。
如要建構更新 Live 活動的酬載,請填入下列程式碼範例中的 apns.payload 欄位,使用 FCM 遠端更新 Live 活動。
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 中試用範例。