Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# BackgroundService

Defines events for background web platform features.

This CDP domain is experimental.

- Types

- Commands

- Events

## Types

Generally, you do not need to instantiate CDP types yourself. Instead, the API creates objects for you as return values from commands, and then you can use those objects as arguments to other commands.

class ServiceName(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

  The Background Service that will be associated with the commands/events. Every Background Service operates independently, but they share the same API.

  BACKGROUND_FETCH = 'backgroundFetch'
  BACKGROUND_SYNC = 'backgroundSync'
  PUSH_MESSAGING = 'pushMessaging'
  NOTIFICATIONS = 'notifications'
  PAYMENT_HANDLER = 'paymentHandler'
  PERIODIC_BACKGROUND_SYNC = 'periodicBackgroundSync'

class EventMetadata(key, value)

  A key-value pair for additional event information to pass along.

  key: `str`
  value: `str`

class BackgroundServiceEvent(timestamp, origin, service_worker_registration_id, service, event_name, instance_id, event_metadata, storage_key)
  timestamp: `TimeSinceEpoch`

    Timestamp of the event (in seconds).

  origin: `str`

    The origin this event belongs to.

  service_worker_registration_id: `RegistrationID`

    The Service Worker ID that initiated the event.

  service: `ServiceName`

    The Background Service this event belongs to.

  event_name: `str`

    A description of the event.

  instance_id: `str`

    An identifier that groups related events together.

  event_metadata: `List`[`EventMetadata`]

    A list of event-specific information.

  storage_key: `str`

    Storage key this event belongs to.

## Commands

Each command is a generator function. The return type `Generator[x, y, z]` indicates that the generator yields arguments of type `x`, it must be resumed with an argument of type `y`, and it returns type `z`. In this library, types `x` and `y` are the same for all commands, and `z` is the return type you should pay attention to. For more information, see Getting Started: Commands.

clear_events(service)

  Clears all stored data for the service.

  Parameters:

    service (`ServiceName`) –

  Return type:

    `Generator`[`Dict`[`str`, `Any`], `Dict`[`str`, `Any`], `None`]

set_recording(should_record, service)

  Set the recording state for the service.

  Parameters:

    - should_record (`bool`) –

    - service (`ServiceName`) –

  Return type:

    `Generator`[`Dict`[`str`, `Any`], `Dict`[`str`, `Any`], `None`]

start_observing(service)

  Enables event updates for the service.

  Parameters:

    service (`ServiceName`) –

  Return type:

    `Generator`[`Dict`[`str`, `Any`], `Dict`[`str`, `Any`], `None`]

stop_observing(service)

  Disables event updates for the service.

  Parameters:

    service (`ServiceName`) –

  Return type:

    `Generator`[`Dict`[`str`, `Any`], `Dict`[`str`, `Any`], `None`]

## Events

Generally, you do not need to instantiate CDP events yourself. Instead, the API creates events for you and then you use the event’s attributes.

class RecordingStateChanged(is_recording, service)

  Called when the recording state for the service has been updated.

  is_recording: `bool`
  service: `ServiceName`

class BackgroundServiceEventReceived(background_service_event)

  Called with all existing backgroundServiceEvents when enabled, and all new events afterwards if enabled and recording.

  background_service_event: `BackgroundServiceEvent`