Iniziare a utilizzare Attività in tempo reale

Con l'API HTTP v1 Firebase Cloud Messaging, puoi inviare, aggiornare e terminare da remoto le notifiche delle attività in tempo reale sui dispositivi iOS. Tieni presente che devi avere iOS 16.1 per utilizzare l'attività live e iOS 17.2 per avviare da remoto una notifica di attività live.

Prima di iniziare

Prima di iniziare a utilizzare le attività in tempo reale su Firebase Cloud Messaging, segui le istruzioni riportate in Configurare un'app client Firebase Cloud Messaging sulle piattaforme Apple per creare e aggiungere Firebase Cloud Messaging alla tua app client.

Avviare un'attività in tempo reale

Per avviare un'attività in tempo reale da remoto utilizzando Firebase Cloud Messaging, devi ottenere un token push-to-start da Apple. Ti servirà anche il token di registrazione FCM per l'app di destinazione.

Per creare un payload che avvii un'attività live, compila il campo apns.payload dal seguente codice di esempio per avviare da remoto un'attività live utilizzando 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

Esegui

Fai clic su Esegui per provare l'esempio in Explorer API.

Aggiornare un'attività in tempo reale

Per aggiornare un'attività in tempo reale da remoto utilizzando Firebase Cloud Messaging, devi ottenere un token push da Apple. Ti servirà anche il token di registrazione FCM per l'app di destinazione.

Per creare un payload che aggiorna un'attività live, compila il campo apns.payload dal seguente esempio di codice per aggiornare da remoto un'attività live utilizzando 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

Esegui

Fai clic su Esegui per provare l'esempio in Explorer API.

Terminare un'attività in tempo reale

Per terminare un'attività in tempo reale utilizzando Firebase Cloud Messaging, devi ottenere un token push da Apple. Ti servirà anche il token di registrazione FCM per l'app di destinazione.

Per creare un payload che termina un'attività live, compila il campo apns.payload dal seguente esempio di codice per terminare da remoto un'attività live utilizzando 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

Esegui

Fai clic su Esegui per provare l'esempio in Explorer API.