0

Unregister a single device for push notifications

by
Published Oct 17, 2025

Unregisters a single device by its device ID. All its subscriptions for receiving push notifications through channels will also be deleted.

Script ably Verified

The script

Submitted by hugo697 Bun
Verified 240 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Unregister a single device for push notifications
7
 * Unregisters a single device by its device ID. All its subscriptions for receiving push notifications through channels will also be deleted.
8
 */
9
export async function main(
10
  auth: Ably,
11
  device_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  X_Ably_Version: string,
14
) {
15
  const url = new URL(
16
    `https://rest.ably.io/push/deviceRegistrations/${device_id}`,
17
  );
18
  for (const [k, v] of [["format", format]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "DELETE",
25
    headers: {
26
      "X-Ably-Version": X_Ably_Version,
27
      Authorization: "Bearer " + auth.apiKey,
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37