import type { Device, ListDevicesQuery, Paginated, SubscribeNotificationsInput } from '../../types'; import type { HttpClient } from '../../http'; /** * Push notification subscriptions: which browsers and apps a user wants * notifications delivered to. * * ```ts * // after the browser has granted permission and Firebase has a token * await crm.notifications.subscribe({ token }); * * // logout, or the user turning notifications off * await crm.notifications.unsubscribe(token); * * // what the user sees in "notification settings" * const { data } = await crm.notifications.devices(); * ``` * * ## What this does and does not cover * * A push subscription has two halves and only one of them is an API call: * * 1. **In the browser** -- ask for permission (`Notification.requestPermission()`), * then get a registration token for this instance * (`getToken(messaging, { vapidKey })` from `firebase/messaging`). This SDK * deliberately does none of that: it would mean a hard dependency on * `firebase/*`, in a package that also runs on Node, to wrap two calls whose * arguments come from your own Firebase project. * 2. **Here** -- hand that token to the platform so the send path can reach it, * which is what {@link subscribe} does, and withdraw it on logout, which is * {@link unsubscribe}. * * ## Where a subscription lives * * On the platform, against the acting user, in this product -- not in the core * platform. Core owns identity and the agency's Firebase credentials; messages * arrive here, so the tokens and the sending live here too, and core is not on * the delivery path. Nothing in the subscription call names the Firebase project: * the platform resolves it from the client the call arrives on and stamps it onto * the row, because a token is only sendable through the project that minted it * and nothing in the token says which. * * ## What a subscribed device then receives * * One notification per **message the business did not send itself** -- a * contact's reply, or an echo of somebody answering from the WhatsApp Business * app -- on a chat the device's owner is responsible for: the assigned agent, or * the account owner while the chat is unassigned. An agent's own reply, a bot's, * a campaign send and a delivery receipt all notify nobody. * * The title is who wrote; the body is the message text, or `Image Message` and * the like for media. `data.chat` is the chat id, `webpush.fcmOptions.link` is * what a browser opens on click, and `data.click_action` is that same URL on web * and the bare chat id on a native app. The payload is documented key by key in * `docs/audience/notifications.md`. */ export declare class Notifications { private readonly http; constructor(http: HttpClient); /** * Subscribes this browser or app instance to notifications. * * ```http * POST /v1/audience/devices * authorization: Bearer * client-id: tpc_8Kd3mXp7qR2wL9zB * content-type: application/json * * { * "token": "fNx8...:APA91bH...", * "platform": "web", * "device_label": "Chrome on Windows", * "tpc_id": "tpc_8Kd3mXp7qR2wL9zB" * } * ``` * * Answers `201` with the stored {@link Device}, token omitted. Of those four * fields the SDK fills in two for you: `platform` defaults to `'web'` when it * can see a DOM, and `device_label` to something like `Chrome on Windows` from * the user agent. * * ## Call it on every app load, not once * * It is an idempotent upsert keyed on the token, so re-sending one the platform * already holds costs a single indexed update. That is the intended usage, * because two things change underneath you: * * - FCM rotates registration tokens on its own schedule, and the old one stops * being delivered to without anyone being told. * - A device unseen for 60 days is pruned, and "seen" means a successful send * or a call to this method. A user who receives no notifications for two * months would otherwise fall off the list. * * Re-registering a token that is already on file against **another** user * moves it rather than duplicating it: one browser has one person signed in, * and the previous owner's notifications must stop reaching it the moment * someone else logs in there. * * ## Identity * * The user comes from the verified token this client was built with, and there * is no field that could say otherwise. `tpc_id` is the one tenant field * accepted, because it is not an identity claim -- it says which reseller * frontend is registering, which decides whose Firebase project and which deep * link host apply. It is cross-checked against the verified `client-id`, so it * can confirm the surface but never move the device to another tenant: a value * that contradicts the token is refused with `403` rather than ignored. * * ## Failures worth handling * * - `400 validation_failed` -- the token is missing or implausible, or * `platform` could not be defaulted (see {@link SubscribeNotificationsInput}). * - `403 forbidden` -- `tpc_id` names a different client than the token was * issued for. * - `503 push_not_configured` -- the agency has no Firebase notification * resource set up. Nothing the caller can fix; notifications are simply off * for this account. * - `503 push_unavailable` -- the platform could not reach core to read the * configuration. Transient. * * Both `503`s deserve the same handling as a successful call: try again on the * next app load, which the advice above means you are doing anyway. * * Scope: `devices:write`. */ subscribe(input: SubscribeNotificationsInput): Promise; /** * Stops notifications reaching one device. What logout calls. * * ```http * POST /v1/audience/devices/unregister * { "token": "fNx8...:APA91bH..." } * ``` * * `204`, no body, whether or not a row went -- the caller is asking for a * guarantee about the future, and a token that is already gone provides it. So * this is safe to call unconditionally on logout without first checking whether * the device was ever subscribed. * * Scoped to the caller's own devices. A token identifies a browser rather than * a person, so by the time one user logs out the row may already belong to * whoever logged in after them; an unscoped delete would cut off the person now * holding it. * * A `POST` with the token in the body, not `DELETE /devices/:token`: a * registration token is hundreds of characters and may contain `:` and `/`, and * putting one in a path would invite proxy length limits and write a live send * credential into every access log on the way. * * ## Also delete it on the device * * This revokes the platform's permission to send to the token. If the user is * turning notifications *off* -- rather than logging out -- also call * `deleteToken(messaging)` in the browser, otherwise the next `getToken()` * hands back the same token and the subscription can come back by accident. * * Scope: `devices:write`. */ unsubscribe(token: string): Promise; /** * The caller's own subscribed devices, newest contact first. * * ```http * GET /v1/audience/devices?page=1&rows=25 * ``` * * Offset paginated, `rows` 1..100 and default 25, sorted by `lastSeenAt` * descending. This is what a "notification settings" screen lists, and the 20 * most recently seen devices are also the only ones a notification is sent to, * so a longer list is itself the answer to "why did my old laptop stop * buzzing". * * Only ever the acting user's own devices -- there is no account-wide view and * no device id in the path. A device is one person's browser, and the only * party with a reason to see or revoke it is that person. * * Tokens are not included, so this cannot tell you whether *this* browser is * subscribed. Decide that locally: the browser knows its own * `Notification.permission` and its own token, and {@link subscribe} being * idempotent means the cheap answer to "am I still subscribed" is to subscribe * again. * * Scope: `devices:read`. */ devices(query?: Partial): Promise>; }