Unregister matching devices for push notifications

Unregisters devices. All their subscriptions for receiving push notifications through channels will also be deleted.

Script ably Verified

by hugo697 ยท 10/17/2025

The script

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