Register a device for receiving push notifications
One script reply has been approved by the moderators Verified

Register a device’s details, including the information necessary to deliver push notifications to it. Requires "push-admin" capability.

Created by hugo697 138 days ago
Submitted by hugo697 Bun
Verified 138 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Register a device for receiving push notifications
7
 * Register a device’s details, including the information necessary to deliver push notifications to it. Requires "push-admin" capability.
8
 */
9
export async function main(
10
  auth: Ably,
11
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
12
  X_Ably_Version: string,
13
  body: {
14
    id?: string;
15
    clientId?: string;
16
    formFactor?:
17
      | "phone"
18
      | "tablet"
19
      | "desktop"
20
      | "tv"
21
      | "watch"
22
      | "car"
23
      | "embedded";
24
    metadata?: {};
25
    platform?: "ios" | "android" | "browser";
26
    deviceSecret?: string;
27
    "push.recipient"?: {
28
      transportType?: "apns" | "fcm" | "gcm" | "web";
29
      deviceToken?: string;
30
      registrationToken?: string;
31
      encryptionKey?: {};
32
      clientId?: string;
33
      deviceId?: string;
34
    };
35
    "push.state"?: "Active" | "Failing" | "Failed";
36
  },
37
) {
38
  const url = new URL(`https://rest.ably.io/push/deviceRegistrations`);
39
  for (const [k, v] of [["format", format]]) {
40
    if (v !== undefined && v !== "" && k !== undefined) {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "POST",
46
    headers: {
47
      "X-Ably-Version": X_Ably_Version,
48
      "Content-Type": "application/json",
49
      Authorization: "Bearer " + auth.apiKey,
50
    },
51
    body: JSON.stringify(body),
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59