# Push devices

> Register phone installs, Home Screen web app subscriptions and signed-out installs for push alerts, report or withdraw a position, and list or remove devices.

API reference · Reference · https://wxalerts.org/docs/api/notifications-devices/

Base URL: `https://api.wxalerts.org`

A push **device** is one install that can be woken with an alert. There are
three kinds. A **phone install** is the Android or iOS app, signed in, which is
matched on the account's saved places and, if it reports one, its position. A
**browser subscription** is the Home Screen web app on an iPhone, subscribed
with the standard Push API, and is matched on saved places only, never on a
position. An **anonymous install** is the app while nobody is signed in, and is
matched on its reported position only. The guide for people is
[Alerts on iPhone without the app](/docs/alerts/iphone-without-the-app/).

Phone and browser routes accept a session cookie or a Bearer token; writes with a
cookie need `X-Requested-With: wxalerts`. The anonymous routes need no account
but always need that header and an `X-Device-Secret`. Responses are
`Cache-Control: no-store` unless a route says otherwise. Sign-in errors and
the `422` format are on the [overview](/docs/api/notifications/).

`device_id` is a string of 1 to 64 characters that the client mints and keeps.
A shorter or longer one gets `400` with
`{"detail": "device_id must be between 1 and 64 characters."}` on the routes
that check it (every write except the three phone routes
`POST` position, `DELETE` position and `DELETE` device, which look it up
instead and answer `404` or `204`).

## Device object

| Field | Type | Description |
| --- | --- | --- |
| `device_id` | string | |
| `platform` | string | `android`, `ios`, or `web` for a browser subscription |
| `enabled` | boolean | whether pushes are sent |
| `app_version` | string or null | |
| `lat`, `lon` | number or null | the last reported position |
| `accuracy_m` | number or null | its accuracy in meters |
| `position_reported_at` | string or null | UTC |
| `position_is_fresh` | boolean | whether that position is recent enough to match on: reported within the last 6 hours (21600 seconds) |
| `prefs` | object | the stored preferences, below |
| `created_at`, `updated_at` | string | UTC |

## Preferences (`prefs`)

The client's filter, reduced to what the server needs to decide whether to
wake a device. Every field is optional with a permissive default, so a device
that registers before its settings load gets too much rather than nothing.

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `significances` | array of `W`, `A`, `Y`, `S` | `["W", "A"]` | significances worth a push. Empty means none |
| `muted_phenomena` | array of string, up to 200 | `[]` | VTEC codes switched off. A deny-list, so an unlisted hazard still gets through |
| `critical_phenomena` | array of string, up to 200 | life-safety codes plus `TR` | codes that break through quiet hours and are sent as a visible notification. This list can only add: the life-safety set (Tornado, Severe Thunderstorm, Flash Flood, Extreme Wind, Hurricane, Storm Surge, Tsunami, Snow Squall and Dust Storm Warnings, and every Emergency) is critical whatever is sent and cannot be muted |
| `quiet_start` | integer 0 to 1439 or null | `null` | minutes past midnight, device local. A start after the end means the window crosses midnight |
| `quiet_end` | integer 0 to 1439 or null | `null` | |
| `audible_phenomena` | array of string, up to 200, or null | `null` | which visible pushes play the warning tone. `null` means never, for builds that predate it |
| `audible_significances` | array of `W`, `A`, `Y`, `S`, or null | `null` | both this and `audible_phenomena` must match |
| `timezone` | string, up to 64, or null | `null` | the handset's IANA zone, such as `America/Chicago`. An unknown name is dropped (stored as `null`), not refused |
| `storm_enabled` | boolean | `false` | send storm pushes (storm approaching) |
| `storm_device` | boolean | `true` | storm pushes for the device's own position |
| `storm_places` | array of string, up to 200, or null | `null` | labels of the saved places that get storm pushes. `null` means every place |
| `sky_capable` | boolean | `false` | this build can draw sunrise and sunset notifications. Devices that do not say so are skipped |
| `outlook_capable` | boolean | `false` | the same for SPC outlook notifications |
| `storm_center_capable` | boolean | `false` | the same for Storm Center (tropical and winter) notifications |

## `GET /notifications/devices`

The account's registered devices, including browser subscriptions.

Access: signed in. No parameters.

| Field | Type | Description |
| --- | --- | --- |
| `devices` | array of device | |
| `max_devices` | integer | the cap per account, 10 |
| `life_safety` | object | the locked life-safety set, as on [saved places](/docs/api/notifications-places/#get-notificationsplaces) |
| `life_safety_events` | array of string | the same set as event names |

## `PUT /notifications/devices/{device_id}`

Register or update a phone install. Idempotent, so it is safe to call on every
launch and whenever the token rotates. It leaves a stored position alone.

Access: signed in, with the CSRF header on a cookie.

Body:

| Field | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fcm_token` | string, 1 to 4096 | yes | | the push token. A token that was registered to another device is moved here |
| `platform` | `android` or `ios` | yes | | |
| `app_version` | string, up to 32 | no | `null` | |
| `enabled` | boolean | no | `true` | |
| `prefs` | object | no | all defaults | see Preferences. It **replaces** the stored preferences |

```json
{
  "fcm_token": "dX3...",
  "platform": "android",
  "app_version": "1.8.2",
  "prefs": { "significances": ["W", "A"], "quiet_start": 1320, "quiet_end": 420, "timezone": "America/Chicago", "storm_enabled": true, "sky_capable": true }
}
```

Response `200`: the device object.

| Status | Body | When |
| --- | --- | --- |
| `400` | `{"detail": "device_id must be between 1 and 64 characters."}` | |
| `409` | `{"detail": "This account already has 10 registered devices, which is the limit. Remove one before adding another."}` | a new device over the cap |
| `409` | `{"detail": "That device_id is a browser subscription on this account. An app install needs its own device_id."}` | the id belongs to a browser subscription |
| `409` | `{"detail": "This device was registered by another request at the same time. Try again."}` | two first registrations raced; retry |
| `422` | standard list | a field out of bounds |

## `POST /notifications/devices/{device_id}/position`

Report where the device is. Its own route because it fires every few minutes
while registration fires on launch.

Access: signed in, with the CSRF header on a cookie.

Body:

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `lat` | number, -90 to 90 | yes | |
| `lon` | number, -180 to 180 | yes | |
| `accuracy_m` | number, 0 to 100000 | no | GPS accuracy in meters. A fix that is really a cell tower guess is ignored by the dispatcher |
| `course_deg` | number, 0 up to but not including 360 | no | heading, degrees clockwise from true north. Sent only while moving |
| `speed_mps` | number, 0 to 90 | no | meters per second |
| `fixed_at` | string, ISO 8601 with a time zone | no | when the fix was taken. Absent, the arrival time stands in |

`course_deg` and `speed_mps` must be sent **together or not at all**; one
without the other is a `422`.

Response `200`: the device object.

| Status | Body | When |
| --- | --- | --- |
| `404` | `{"detail": "That device is not registered. Register it before reporting a position."}` | |
| `409` | `{"detail": "A browser subscription is matched on your saved places only and does not report a position."}` | the id is a browser subscription |
| `422` | standard list, or a `value_error` for unpaired `course_deg` and `speed_mps` | |

## `DELETE /notifications/devices/{device_id}/position`

Stop sharing the device's position. Withdrawing location consent is its own
action: saved places stay watched, and the stored position stops matching
immediately instead of for the rest of its six hour window.

Access: signed in, with the CSRF header on a cookie. Response `200`: the device
object with `lat`, `lon` and `position_reported_at` cleared. `404` with
`{"detail": "That device is not registered."}`.

## `DELETE /notifications/devices/{device_id}`

Forget an install, phone or browser. Idempotent: a device that is already gone
answers `204` too.

Access: signed in, with the CSRF header on a cookie. Response `204`, no body.

## `GET /notifications/web-push/key`

Whether browser push is offered, and the key to subscribe with.

Access: anonymous. No parameters. Cached: `Cache-Control: public, max-age=300`.

| Field | Type | Description |
| --- | --- | --- |
| `available` | boolean | `false` when browser push is switched off, so a site can leave the offer out |
| `public_key` | string or null | the application server key for `pushManager.subscribe()`, base64url |

```json
{ "available": true, "public_key": "BNc..." }
```

## `PUT /notifications/web-push/devices/{device_id}`

Register or update a browser's push subscription. Send the browser's
`PushSubscription.toJSON()`. Call it whenever the page finds itself
subscribed, not only the first time, because a browser may replace a
subscription on its own.

Access: signed in (account only, there is no anonymous form), with the CSRF
header on a cookie.

Body:

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `endpoint` | string, 1 to 2048 | yes | the push service URL |
| `keys.p256dh` | string, 1 to 128 | yes | the client public key, which must decode to a P-256 point |
| `keys.auth` | string, 1 to 64 | yes | the 16 byte auth secret, base64url |
| `timezone` | string, up to 64 | no | the phone's IANA zone. Without it, alert times are written in UTC. An unknown name is dropped |
| `app_version` | string, up to 32 | no | |

`expirationTime` in `toJSON()` output is ignored.

Response `200`: the device object. A browser subscription is matched on the
account's saved places within the plan's budget, never on a position.

| Status | Body | When |
| --- | --- | --- |
| `400` | `{"detail": "device_id must be between 1 and 64 characters."}` | |
| `409` | `{"detail": "That device_id is an app install on this account. A browser subscription needs its own device_id."}` | the id belongs to an app install |
| `409` | the device cap or race message, as for `PUT /notifications/devices/{device_id}` | |
| `422` | `{"detail": "..."}` | the subscription is not one this server sends to (a bad endpoint or key) |
| `422` | standard list | a field out of bounds |
| `503` | `{"detail": "Alerts in the browser are not available right now."}` | browser push is switched off |

## Anonymous installs

These four routes take an `X-Device-Secret` header and no
account. The secret is 32 to 128 lowercase hexadecimal characters that the
app minted on first run. The first registration of a `device_id` stores it and
every later call must present the same one. The header `X-Requested-With:
wxalerts` is required.

Limits, per client address, on every anonymous route: registration allows 30
requests a minute with a burst of 10; position and delete calls share 120 a
minute with a burst of 60. Past it you get `429` with
`{"detail": "Too many requests from this address. Try again shortly."}` and
`Retry-After` and `Cache-Control: no-store` headers.

Errors common to all four:

| Status | Body | When |
| --- | --- | --- |
| `400` | `{"detail": "device_id must be between 1 and 64 characters."}` | |
| `401` | `{"detail": "Missing X-Device-Secret header. Anonymous devices must send their secret."}` | |
| `401` | `{"detail": "X-Device-Secret must be 32 to 128 lowercase hexadecimal characters."}` | |
| `403` | `{"detail": "That device secret does not match this device."}` | the device exists and the secret is wrong |
| `403` | `{"detail": "Missing X-Requested-With header."}` | |
| `429` | as above | over the limit |

### `PUT /notifications/anonymous/devices/{device_id}`

Register or update a signed-out install. Same body as the account route
(`fcm_token`, `platform`, `app_version`, `enabled`, `prefs`). Response `200`:
the device object. It adds `409` with
`{"detail": "This device was registered by another request at the same time. Try again."}`
for a registration race.

### `POST /notifications/anonymous/devices/{device_id}/position`

Report where this install is. Same body as the account position route
(`lat`, `lon`, `accuracy_m`, `course_deg` with `speed_mps`, `fixed_at`).
Response `200`: the device object. `404` with
`{"detail": "That device is not registered. Register it before reporting a position."}`.

### `DELETE /notifications/anonymous/devices/{device_id}/position`

Stop sharing where this install is. Response `200`: the device object with
the position cleared. `404` with `{"detail": "That device is not registered."}`.

### `DELETE /notifications/anonymous/devices/{device_id}`

Forget this install. Response `204`. A device that is already gone answers
`204`, but a device that exists with a wrong secret answers `403`, so a caller
cannot silence somebody else's phone.
