Skip to content

Custom Interfaces ​

The useNotifications() composable in Vue and hook in React return the state of the notification center and its actions. You may use it to build your own bell or panel, or to add a Mark all as read button to a page:

vue
<script setup>
import { useNotifications } from '@inertiaui/notifications-vue'

const { counts, markAllAsRead } = useNotifications()
</script>

<template>
    <button type="button" :disabled="counts.unread === 0" @click="markAllAsRead()">Mark all as read</button>
</template>
jsx
import { useNotifications } from '@inertiaui/notifications-react'

export default function MarkAllAsRead() {
    const { counts, markAllAsRead } = useNotifications()

    return (
        <button type="button" disabled={counts.unread === 0} onClick={() => markAllAsRead()}>
            Mark all as read
        </button>
    )
}

With server-side rendering, the first render matches the server's HTML, and the current state shows once the component mounts. Code that reads inboxStore with React's useSyncExternalStore should pass inboxStore.getServerSnapshot as its third argument.

The composable and hook return the following state. In Vue, each value is a computed ref. In React, each value is a plain value:

StateDescription
counts{ unread, unseen }, leaving out archived and snoozed notifications.
itemsThe loaded and arrived notifications, newest first, with the user's changes applied.
arrivedThe notifications that arrived in real time, newest first.
filter, categoryThe selected filter, and the selected category or null.
categoryCountsThe unread count of each category that has unread notifications.
status'idle', 'loading', 'ready', or 'error'.
loadingMore, hasMoreWhether the next page is loading, and whether there is one.
moreFailedWhether loading the next page failed.
pendingActionsThe index of the running action of each notification, by notification ID.
urlThe URL of the JSON endpoints, or undefined while no user is signed in.
userThe user of the shared prop, or undefined while no user is signed in.
channelThe private channel of the user's broadcast notifications, or null.
announcementsChannelThe private channel of published announcements, or null unless they are broadcast.
allowedActionsThe actions that the user may take besides reading: 'snooze', 'archive', 'mute', and 'delete'.
selectableWhether the user may select notifications to change them together.
categoryFilterThe categories to filter by, as labels by key.
tabsThe custom tabs by key, each with its label, category, and data.
pausedUntilThe ISO 8601 time that notifications are paused until, or null while they are not paused. It turns null by itself once the time passes.
canPauseWhether your application offers pausing. While it does not, pausedUntil is null.
pausePresetsThe pause times of the pause.presets option: numbers of minutes, and 'tomorrow'.

It also returns the following actions:

ActionDescription
load(filter, category)Loads the first page of the filter, in one category, or in every category with null.
loadMore({ retry })Loads the next page. After a failure, it only loads again with retry: true.
opened(tabs, { markAllAsSeen, categories })Call when your panel opens. It loads the list when needed and marks it as seen. Every panel shares one list, so pass the tabs and category keys of your panel to switch away from a tab or category that another panel selected. To mark only what your panel shows as seen, pass markAllAsSeen: false and call batch('seen', items). Until closed(), arriving notifications show no toast, and opening closes the toasts of notifications.
closed({ markAllAsSeen })Call when your panel closes, with the options it opened with.
refresh()Requests the counts.
receive(item, counts)Adds a notification from your own Echo listener. Pass the broadcast's counts too, so a collapsed notification is not counted twice.
receiveChange(payload)Applies an InboxChanged broadcast, with its change and counts, from your own Echo listener.
markAsRead(item), markAsUnread(item)Changes one notification. markAsRead() also takes the ID of a notification that no list shows.
archive(item), unarchive(item)Moves one notification to the archive, or back.
snooze(item, until), unsnooze(item)Hides one notification until a Date or ISO 8601 time, or moves it back.
delete(item)Deletes one notification for good.
mute(item), unmute(item)Mutes or unmutes the thread of the notification's group key.
pause(until), resume()Pauses notifications until a future Date or ISO 8601 time, or resumes them. They do nothing while canPause is false.
complete(item, index)Saves the result of the action at the index. Also takes the ID of a notification that no list shows.
batch(action, items, until)Runs 'read', 'unread', 'seen', 'archive', 'unarchive', 'snooze', 'unsnooze', or 'delete' on the given notifications. Snoozing needs the until time, a Date or an ISO 8601 string. While selectable is false, only 'seen' runs.
markAllAsRead({ category })Marks the notifications of the selected category, or of every category, as read, up to the newest one loaded. A category option, or null for every category, takes the place of the selected one.
markAllAsSeen()Marks every notification as seen, up to the newest one loaded.
archiveAll({ onlyRead, category })Archives the notifications of the selected category, or of every category, up to the newest one loaded. With onlyRead: true, it only archives the read ones. It takes the category option of markAllAsRead().
open(item, event)Marks the notification as read and visits its URL, if it has one. A click that opens a new tab is left to the browser.
runAction(item, index, event)Marks the notification as read and runs one of its actions, saving its result once it succeeds.
toastThe toast() function.

Every change shows right away, and a failed request undoes only its own change. The actions that snooze, archive, mute, or delete do nothing while your application does not offer that action, such as delete(item) by default. The same goes for archiveAll() and for those actions of batch().

JSON Endpoints ​

The Route::inertiaNotifications method registers the following routes, named inertia-notifications.*, such as inertia-notifications.index. Except for /preferences and the /push routes, every response contains the new counts, with the count keys of the shared prop, such as unread, unseen, and categories. The response of each of these POST routes also contains the change that the browser shares with its other tabs.

MethodPathDescription
GET/One page of items, with nextCursor. Accepts filter, category, cursor, and per_page (1 to 100).
GET/countsThe counts.
GET/{id}One notification or announcement of the user, as item.
POST/{id}/read, /{id}/unreadMarks one notification as read or unread.
POST/{id}/archive, /{id}/unarchiveArchives one notification, or moves it back.
POST/{id}/snooze, /{id}/unsnoozeSnoozes one notification until the future until time, or moves it back.
POST/{id}/completeSaves the result of the action at the action index.
POST/{id}/mute, /{id}/unmuteMutes or unmutes the thread of the notification's group key. A notification without a group key gets a 422 response.
POST/{id}/deleteDeletes one notification.
POST/readMarks every unread notification as read.
POST/seenMarks every notification as seen.
POST/seen/itemsMarks up to 100 notifications and announcements in ids as seen, such as the ones that a panel showed.
POST/archiveArchives every notification, or only the read ones with read: true.
POST/batchRuns the action on up to 100 notifications in ids: read, unread, seen, archive, unarchive, snooze, unsnooze, or delete. The snooze action needs an until time in the future. Not found while users may not select notifications.
POST/pause, /resumePauses the user's notifications until the future until time, or resumes them.
GET, POST/preferencesReads or saves the user's preferences.
POST/push/subscribe, /push/unsubscribeSaves the browser's push subscription for the user, or removes the one of the endpoint, and answers subscribed. Not found while push is off.
GET/push/devicesThe user's devices with push, in the devices format of usePush(). Not found while push is off.
POST/push/devices/{device}/deleteRemoves the subscription of one of the user's devices by its numeric ID, and answers removed. A device of another user stays. Not found while push is off.

The /read, /seen, and /archive endpoints accept a category, a before time, and the ids of the newest notifications the browser had, up to 1000. They change the notifications created before before, and those created at that second whose IDs are listed, so a notification that arrived in the same second is left alone.

The endpoints of an action that your application does not offer answer with a 404 response, such as /{id}/delete by default, and so does /batch for that action. The GET / endpoint answers with a 422 response for a filter that the list does not offer, such as snoozed while snoozing is not offered.

The {id} of a path and the ids of a request are the UUIDs of notifications and announcements. A path with another ID is not found, and ids with another ID fail validation.

The method also registers two routes that do not answer with JSON: GET /{id}/open, which opens a notification from its push and redirects to its URL, and the service worker of web push at /push/worker.js. Both stay outside the groups that you register the routes in, such as a team prefix. The /{id}/open route runs the web middleware and the middleware of the other routes. The worker runs neither, and like the other /push routes, it is not found while push is off.