1 | |
2 | type Ably = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Publish a push notification to device(s) |
7 | * A convenience endpoint to deliver a push notification payload to a single device or set of devices identified by their client identifier. |
8 | */ |
9 | export async function main( |
10 | auth: Ably, |
11 | format: "json" | "jsonp" | "msgpack" | "html" | undefined, |
12 | X_Ably_Version: string, |
13 | body: { |
14 | recipient: { |
15 | transportType?: "apns" | "fcm" | "gcm" | "web"; |
16 | deviceToken?: string; |
17 | registrationToken?: string; |
18 | encryptionKey?: {}; |
19 | clientId?: string; |
20 | deviceId?: string; |
21 | }; |
22 | push?: { |
23 | data?: string; |
24 | notification?: { |
25 | title?: string; |
26 | body?: string; |
27 | icon?: string; |
28 | sound?: string; |
29 | collapseKey?: string; |
30 | }; |
31 | apns?: { |
32 | notification?: { |
33 | title?: string; |
34 | body?: string; |
35 | icon?: string; |
36 | sound?: string; |
37 | collapseKey?: string; |
38 | }; |
39 | }; |
40 | fcm?: { |
41 | notification?: { |
42 | title?: string; |
43 | body?: string; |
44 | icon?: string; |
45 | sound?: string; |
46 | collapseKey?: string; |
47 | }; |
48 | }; |
49 | web?: { |
50 | notification?: { |
51 | title?: string; |
52 | body?: string; |
53 | icon?: string; |
54 | sound?: string; |
55 | collapseKey?: string; |
56 | }; |
57 | }; |
58 | }; |
59 | }, |
60 | ) { |
61 | const url = new URL(`https://rest.ably.io/push/publish`); |
62 | for (const [k, v] of [["format", format]]) { |
63 | if (v !== undefined && v !== "" && k !== undefined) { |
64 | url.searchParams.append(k, v); |
65 | } |
66 | } |
67 | const response = await fetch(url, { |
68 | method: "POST", |
69 | headers: { |
70 | "X-Ably-Version": X_Ably_Version, |
71 | "Content-Type": "application/json", |
72 | Authorization: "Bearer " + auth.apiKey, |
73 | }, |
74 | body: JSON.stringify(body), |
75 | }); |
76 | if (!response.ok) { |
77 | const text = await response.text(); |
78 | throw new Error(`${response.status} ${text}`); |
79 | } |
80 | return await response.json(); |
81 | } |
82 |
|