//native
type Ably = {
apiKey: string;
};
/**
* Register a device for receiving push notifications
* Register a device’s details, including the information necessary to deliver push notifications to it. Requires "push-admin" capability.
*/
export async function main(
auth: Ably,
format: "json" | "jsonp" | "msgpack" | "html" | undefined,
X_Ably_Version: string,
body: {
id?: string;
clientId?: string;
formFactor?:
| "phone"
| "tablet"
| "desktop"
| "tv"
| "watch"
| "car"
| "embedded";
metadata?: {};
platform?: "ios" | "android" | "browser";
deviceSecret?: string;
"push.recipient"?: {
transportType?: "apns" | "fcm" | "gcm" | "web";
deviceToken?: string;
registrationToken?: string;
encryptionKey?: {};
clientId?: string;
deviceId?: string;
};
"push.state"?: "Active" | "Failing" | "Failed";
},
) {
const url = new URL(`https://rest.ably.io/push/deviceRegistrations`);
for (const [k, v] of [["format", format]]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"X-Ably-Version": X_Ably_Version,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.apiKey,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 138 days ago