use-truapi
Host

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

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
ParameterType
idnumber
Returns

Promise<void>


context

context: unknown;
Inherited from
NamedMutation.context

data

data: number | undefined;

The last successfully resolved data for the mutation.

Inherited from
NamedMutation.data

error

error: Error | null;

The error object for the mutation, if an error was encountered.

  • Defaults to null.
Inherited from
NamedMutation.error

failureCount

failureCount: number;
Inherited from
NamedMutation.failureCount

failureReason

failureReason: Error | null;
Inherited from
NamedMutation.failureReason

isError

isError: boolean;

A boolean variable derived from status.

  • true if the last mutation attempt resulted in an error.
Inherited from
NamedMutation.isError

isIdle

isIdle: boolean;

A boolean variable derived from status.

  • true if the mutation is in its initial state prior to executing.
Inherited from
NamedMutation.isIdle

isPaused

isPaused: boolean;
Inherited from
NamedMutation.isPaused

isPending

isPending: boolean;

A boolean variable derived from status.

  • true if the mutation is currently executing.
Inherited from
NamedMutation.isPending

isSuccess

isSuccess: boolean;

A boolean variable derived from status.

  • true if the last mutation attempt was successful.
Inherited from
NamedMutation.isSuccess

push

push: (input) => Promise<number>;
Parameters
ParameterType
inputHostPushNotificationRequest
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.reset

status

status: "idle" | "success" | "error" | "pending";

The status of the mutation.

  • Will be:
    • idle initial status prior to the mutation function executing.
    • pending if the mutation is currently executing.
    • error if the last mutation attempt resulted in an error.
    • success if the last mutation attempt was successful.
Inherited from
NamedMutation.status

submittedAt

submittedAt: number;
Inherited from
NamedMutation.submittedAt

variables

variables: HostPushNotificationRequest | undefined;

The variables object passed to the mutationFn.

Inherited from
NamedMutation.variables

Vue

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
ParameterType
idnumber
Returns

Promise<void>


context

readonly context: Ref<unknown, unknown>;
Inherited from
NamedMutation.context

data

readonly data: Ref<undefined, undefined> | Ref<number, number>;

The last successfully resolved data for the mutation.

Inherited from
NamedMutation.data

error

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.error

failureCount

readonly failureCount: Ref<number, number>;
Inherited from
NamedMutation.failureCount

failureReason

readonly failureReason: Ref<Error | null, Error | null>;
Inherited from
NamedMutation.failureReason

isError

readonly isError: Ref<false, false> | Ref<true, true>;

A boolean variable derived from status.

  • true if the last mutation attempt resulted in an error.
Inherited from
NamedMutation.isError

isIdle

readonly isIdle: Ref<false, false> | Ref<true, true>;

A boolean variable derived from status.

  • true if the mutation is in its initial state prior to executing.
Inherited from
NamedMutation.isIdle

isPaused

readonly isPaused: Ref<boolean, boolean>;
Inherited from
NamedMutation.isPaused

isPending

readonly isPending: Ref<false, false> | Ref<true, true>;

A boolean variable derived from status.

  • true if the mutation is currently executing.
Inherited from
NamedMutation.isPending

isSuccess

readonly isSuccess: Ref<false, false> | Ref<true, true>;

A boolean variable derived from status.

  • true if the last mutation attempt was successful.
Inherited from
NamedMutation.isSuccess

push

push: (input) => Promise<number>;
Parameters
ParameterType
inputHostPushNotificationRequest
Returns

Promise<number>


reset

reset: () => void;
Returns

void

Inherited from
NamedMutation.reset

status

readonly status: 
  | Ref<"idle", "idle">
  | Ref<"pending", "pending">
  | Ref<"error", "error">
| Ref<"success", "success">;

The status of the mutation.

  • Will be:
    • idle initial status prior to the mutation function executing.
    • pending if the mutation is currently executing.
    • error if the last mutation attempt resulted in an error.
    • success if the last mutation attempt was successful.
Inherited from
NamedMutation.status

submittedAt

readonly submittedAt: Ref<number, number>;
Inherited from
NamedMutation.submittedAt

variables

readonly variables: 
  | Ref<undefined, undefined>
| Ref<HostPushNotificationRequest, HostPushNotificationRequest>;

The variables object passed to the mutationFn.

Inherited from
NamedMutation.variables

On this page