useNotifications
Schedule and cancel host push notifications (RFC-0019)
useNotifications exposes the host's RFC-0019 notification manager. Call
push(input) to deliver or schedule a notification; it resolves to a
NotificationId you can later pass to cancel(id) to cancel a pending
scheduled notification. scheduledAt is a Unix timestamp in milliseconds;
omit it for immediate delivery. The push mutation's state lives on the
returned object itself: isPending while a push is in flight, data holds
the last id, error the last failure, reset() clears them.
This is host-only: there is no browser fallback, and standalone both push
and cancel reject with HostUnavailableError. Gate the whole feature on
useIsHost. Failures such as the host's
pending-notification cap land in error.
Usage
import { useIsHost, useNotifications } from "@use-truapi/react";
import { useState } from "react";
function AuctionReminder({ endsAt }: { endsAt: bigint }) {
const isHost = useIsHost();
const { push, cancel, isPending, error } = useNotifications();
const [id, setId] = useState<number | null>(null);
async function onRemind() {
const notificationId = await push({
text: "Auction ends in 10 minutes, place your final bid!",
deeplink: "https://auctions.dot/lot/42",
scheduledAt: endsAt - 600_000n,
});
setId(notificationId);
}
async function onCancel() {
if (id !== null) await cancel(id);
setId(null);
}
if (!isHost) return null;
return (
<>
{id === null ? (
<button type="button" onClick={onRemind} disabled={isPending}>
{isPending ? "Scheduling…" : "Remind me"}
</button>
) : (
<button type="button" onClick={onCancel}>Cancel reminder</button>
)}
{error && <p role="alert">{error.message}</p>}
</>
);
}<script setup lang="ts">
import { useIsHost, useNotifications } from "@use-truapi/vue";
import { ref } from "vue";
const props = defineProps<{ endsAt: bigint }>();
const isHost = useIsHost();
const { push, cancel, isPending, error } = useNotifications();
const id = ref<number | null>(null);
async function onRemind() {
id.value = await push({
text: "Auction ends in 10 minutes, place your final bid!",
deeplink: "https://auctions.dot/lot/42",
scheduledAt: props.endsAt - 600_000n,
});
}
async function onCancel() {
if (id.value !== null) await cancel(id.value);
id.value = null;
}
</script>
<template>
<template v-if="isHost">
<button v-if="id === null" type="button" :disabled="isPending" @click="onRemind">
{{ isPending ? "Scheduling…" : "Remind me" }}
</button>
<button v-else type="button" @click="onCancel">Cancel reminder</button>
<p v-if="error" role="alert">{{ error.message }}</p>
</template>
</template>For a fire-and-forget immediate notification, swallow the promise; failures
still show up in error:
<button type="button" onClick={() => void push({ text: "Draft saved" }).catch(() => {})}>
Notify me
</button>See also
useIsHosthides the feature outside a host.useDevicePermissionrequests the"Notifications"device permission first.useHostNavigateuses the same deep-link scheme asdeeplink.
API reference
React
function useNotifications(): NotificationsApi;RFC-0019 scheduled push notifications (host-only; throws HostUnavailableError standalone).
Returns
NotificationsApi
Extends
NamedMutation<NotificationId,Parameters<HostController["pushNotification"]>[0]>
Properties
cancel
cancel: (id) => Promise<void>;Parameters
| Parameter | Type |
|---|---|
id | number |
Returns
Promise<void>
context
context: unknown;Inherited from
NamedMutation.contextdata
data: number | undefined;The last successfully resolved data for the mutation.
Inherited from
NamedMutation.dataerror
error: Error | null;The error object for the mutation, if an error was encountered.
- Defaults to
null.
Inherited from
NamedMutation.errorfailureCount
failureCount: number;Inherited from
NamedMutation.failureCountfailureReason
failureReason: Error | null;Inherited from
NamedMutation.failureReasonisError
isError: boolean;A boolean variable derived from status.
trueif the last mutation attempt resulted in an error.
Inherited from
NamedMutation.isErrorisIdle
isIdle: boolean;A boolean variable derived from status.
trueif the mutation is in its initial state prior to executing.
Inherited from
NamedMutation.isIdleisPaused
isPaused: boolean;Inherited from
NamedMutation.isPausedisPending
isPending: boolean;A boolean variable derived from status.
trueif the mutation is currently executing.
Inherited from
NamedMutation.isPendingisSuccess
isSuccess: boolean;A boolean variable derived from status.
trueif the last mutation attempt was successful.
Inherited from
NamedMutation.isSuccesspush
push: (input) => Promise<number>;Parameters
| Parameter | Type |
|---|---|
input | HostPushNotificationRequest |
Returns
Promise<number>
reset
reset: () => void;A function to clean the mutation internal state (i.e., it resets the mutation to its initial state).
Returns
void
Inherited from
NamedMutation.resetstatus
status: "idle" | "success" | "error" | "pending";The status of the mutation.
- Will be:
idleinitial status prior to the mutation function executing.pendingif the mutation is currently executing.errorif the last mutation attempt resulted in an error.successif the last mutation attempt was successful.
Inherited from
NamedMutation.statussubmittedAt
submittedAt: number;Inherited from
NamedMutation.submittedAtvariables
variables: HostPushNotificationRequest | undefined;The variables object passed to the mutationFn.
Inherited from
NamedMutation.variablesVue
function useNotifications(): NotificationsApi;RFC-0019 scheduled push notifications (host-only; throws HostUnavailableError standalone).
Returns
NotificationsApi
Extends
NamedMutation<NotificationId,Parameters<HostController["pushNotification"]>[0]>
Properties
cancel
cancel: (id) => Promise<void>;Parameters
| Parameter | Type |
|---|---|
id | number |
Returns
Promise<void>
context
readonly context: Ref<unknown, unknown>;Inherited from
NamedMutation.contextdata
readonly data: Ref<undefined, undefined> | Ref<number, number>;The last successfully resolved data for the mutation.
Inherited from
NamedMutation.dataerror
readonly error: Ref<null, null> | Ref<Error, Error>;The error object for the mutation, if an error was encountered.
- Defaults to
null.
Inherited from
NamedMutation.errorfailureCount
readonly failureCount: Ref<number, number>;Inherited from
NamedMutation.failureCountfailureReason
readonly failureReason: Ref<Error | null, Error | null>;Inherited from
NamedMutation.failureReasonisError
readonly isError: Ref<false, false> | Ref<true, true>;A boolean variable derived from status.
trueif the last mutation attempt resulted in an error.
Inherited from
NamedMutation.isErrorisIdle
readonly isIdle: Ref<false, false> | Ref<true, true>;A boolean variable derived from status.
trueif the mutation is in its initial state prior to executing.
Inherited from
NamedMutation.isIdleisPaused
readonly isPaused: Ref<boolean, boolean>;Inherited from
NamedMutation.isPausedisPending
readonly isPending: Ref<false, false> | Ref<true, true>;A boolean variable derived from status.
trueif the mutation is currently executing.
Inherited from
NamedMutation.isPendingisSuccess
readonly isSuccess: Ref<false, false> | Ref<true, true>;A boolean variable derived from status.
trueif the last mutation attempt was successful.
Inherited from
NamedMutation.isSuccesspush
push: (input) => Promise<number>;Parameters
| Parameter | Type |
|---|---|
input | HostPushNotificationRequest |
Returns
Promise<number>
reset
reset: () => void;Returns
void
Inherited from
NamedMutation.resetstatus
readonly status:
| Ref<"idle", "idle">
| Ref<"pending", "pending">
| Ref<"error", "error">
| Ref<"success", "success">;The status of the mutation.
- Will be:
idleinitial status prior to the mutation function executing.pendingif the mutation is currently executing.errorif the last mutation attempt resulted in an error.successif the last mutation attempt was successful.
Inherited from
NamedMutation.statussubmittedAt
readonly submittedAt: Ref<number, number>;Inherited from
NamedMutation.submittedAtvariables
readonly variables:
| Ref<undefined, undefined>
| Ref<HostPushNotificationRequest, HostPushNotificationRequest>;The variables object passed to the mutationFn.
Inherited from
NamedMutation.variables