xtr-dev

payload notifications

by xtr-dev
Notifications & messaging
v3
^3.37.0

Overview

Summary generated from the README

Adds a notifications collection with titles, rich text messages, channels and personal or broadcast audiences. It also registers collections for read receipts, channel subscriptions and push subscriptions, with default access rules and REST endpoints for read state and opt-outs. Web push is optional and needs VAPID keys. Requires Payload 3.37 or newer, React, and a users collection with an admin role.

Features

  • Notifications collection with title, rich text message, channel and personal or broadcast audience
  • Per-user read state, including shared broadcasts tracked through separate read receipts
  • Per-user channel subscriptions with REST endpoints and server helpers
  • Optional web push delivery that respects channel opt-outs
  • Default access rules with admin override and recipient-editable field configuration
  • Collection overrides to customize each generated collection

Use cases

  • Send order status updates to individual users in an app built on Payload
  • Broadcast announcements while letting users opt out of channels such as marketing
  • Build an in-app notification inbox with read and unread tracking and browser push

From the author

A PayloadCMS plugin that adds a configurable notifications collection for sending channel-targeted messages with titles and rich text content

Installation

npm i @xtr-dev/payload-notifications

1

Stars

0

Forks

0

Open Issues

1 weeks ago

Last Updated

469

Weekly Downloads

v0.3.0

npm Version

289.8 KB

Unpacked Size

2

Dependencies

GitHub stars

10 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20261
Dec 4, 20261
Dec 3, 20261
Dec 2, 20261
Dec 1, 20261
Nov 30, 20261
Nov 29, 20261
Nov 28, 20261
Nov 27, 20261
Nov 26, 20261
Nov 25, 20261
Nov 24, 20261
Nov 23, 20261
Nov 22, 20261
Nov 21, 20261
Nov 20, 20261
Nov 19, 20261
Nov 18, 20261
Nov 17, 20261
Nov 16, 20261
Nov 15, 20261
Nov 14, 20261
Nov 13, 20261
Nov 12, 20261
Nov 11, 20261
Nov 10, 20261
Nov 9, 20261
Nov 8, 20261
Nov 7, 20261
Nov 6, 20261
Nov 5, 20261
Nov 4, 20261
Nov 3, 20261
Nov 2, 20261
Nov 1, 20261
Oct 31, 20261
Oct 30, 20261
Oct 29, 20261
Oct 28, 20261
Oct 27, 20261
Oct 26, 20261
Oct 25, 20261
Oct 24, 20261
Oct 23, 20261
Oct 22, 20261
Oct 21, 20261
Oct 20, 20261
Oct 19, 20261
Oct 18, 20261
Oct 17, 20261
Oct 16, 20261
Oct 15, 20261
Oct 14, 20261
Oct 13, 20261
Oct 12, 20261
Oct 11, 20261
Oct 10, 20261
Oct 9, 20261
Oct 8, 20261
Oct 7, 20261
Oct 6, 20261
Oct 5, 20261
Oct 4, 20261
Oct 3, 20261
Oct 2, 20261
Oct 1, 20261
Sep 30, 20261
Sep 29, 20261
Sep 28, 20261
Sep 27, 20261
Sep 26, 20261
Sep 25, 20261
Sep 24, 20261
Sep 23, 20261
Sep 22, 20261
Sep 21, 20261
Sep 20, 20261
Sep 19, 20261
Sep 18, 20261
Sep 17, 20261
Sep 16, 20261
Sep 15, 20261
Sep 14, 20261
Sep 13, 20261
Sep 12, 20261
Sep 11, 20261
Sep 10, 20261
Sep 9, 20261
Sep 8, 20261
Sep 7, 20261
Sep 6, 20261
npm weekly downloads

654+619 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 2026654
Dec 4, 2026813
Dec 3, 2026808
Dec 2, 2026782
Dec 1, 2026825
Nov 30, 2026872
Nov 29, 2026644
Nov 28, 2026498
Nov 27, 2026324
Nov 26, 2026355
Nov 25, 2026385
Nov 24, 2026331
Nov 23, 2026250
Nov 22, 2026147
Nov 21, 202698
Nov 20, 2026101
Nov 19, 202697
Nov 18, 202690
Nov 17, 2026114
Nov 16, 2026162
Nov 15, 2026165
Nov 14, 2026166
Nov 13, 2026210
Nov 12, 2026242
Nov 11, 2026251
Nov 10, 2026227
Nov 9, 2026182
Nov 8, 2026175
Nov 7, 2026181
Nov 6, 2026157
Nov 5, 202694
Nov 4, 202658
Nov 3, 202648
Nov 2, 202644
Nov 1, 202643
Oct 31, 202629
Oct 30, 20265
Oct 29, 20265
Oct 28, 20265
Oct 27, 20266
Oct 26, 202620
Oct 25, 202653
Oct 24, 2026103
Oct 23, 2026103
Oct 22, 2026104
Oct 21, 2026108
Oct 20, 2026107
Oct 19, 202693
Oct 18, 202657
Oct 17, 20266
Oct 16, 20266
Oct 15, 202664
Oct 14, 2026103
Oct 13, 2026135
Oct 12, 2026149
Oct 11, 2026162
Oct 10, 2026163
Oct 9, 2026165
Oct 8, 2026107
Oct 7, 202664
Oct 6, 202632
Oct 5, 202622
Oct 4, 20269
Oct 3, 202614
Oct 2, 202617
Oct 1, 202616
Sep 30, 202621
Sep 29, 202630
Sep 28, 202626
Sep 27, 202625
Sep 26, 202622
Sep 25, 202618
Sep 24, 202622
Sep 23, 202617
Sep 22, 20269
Sep 21, 202623
Sep 20, 202625
Sep 19, 202622
Sep 18, 202622
Sep 17, 202621
Sep 16, 202622
Sep 15, 202621
Sep 14, 20267
Sep 13, 20265
Sep 12, 20267
Sep 11, 202611
Sep 10, 202614
Sep 9, 202616
Sep 8, 202621
Sep 7, 202628
Sep 6, 202635

Each point is npm's rolling 7-day download count.

Health Score

69/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1055701972-root.svg)](https://payloaddirectory.dev/plugins/1055701972-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1055701972-root"><img src="https://payloaddirectory.dev/badge/1055701972-root.svg" alt="Payload Directory"></a>
Updated Nov 28, 2026Created Sep 12, 2025Published Nov 27, 2026

README

@xtr-dev/payload-notifications

npm version

A PayloadCMS plugin that adds a notifications collection with titles, rich text messages, channels, per-user channel subscriptions, personal and broadcast audiences, and per-user read state. Web push delivery is optional (see WEBPUSH.md).

v0.x — minor versions may contain breaking changes until 1.0.

Installation

pnpm add @xtr-dev/payload-notifications

Requires payload ^3.37.0 and react ^18.2.0 || ^19.0.0. The plugin expects a users collection; users with role === 'admin' get full access.

Usage

import { buildConfig } from 'payload'
import { notificationsPlugin } from '@xtr-dev/payload-notifications'

export default buildConfig({
  plugins: [
    notificationsPlugin({
      channels: [
        { id: 'orders', name: 'Order Updates', description: 'Order status and shipping' },
        { id: 'marketing', name: 'Marketing' },
      ],
    }),
  ],
})

Called without options, notificationsPlugin() uses a single built-in default channel. Passing an empty channels array throws.

Options

OptionTypeDefaultDescription
channelsNotificationChannel[] ({ id, name, description? })one default channelChoices for a notification's channel, a user's channel subscriptions and a push subscription's channels
webPushWebPushConfigundefinedVAPID keys and push behaviour, see WEBPUSH.md
collectionOverrides{ notifications?, pushSubscriptions?, notificationReads?, channelSubscriptions? }undefinedFunctions that receive and return each generated CollectionConfig
recipientEditableFieldsstring[][]Fields of notifications a recipient may update on their own notification, besides isRead/readAt. See Recipient-editable fields

Collections

notifications

The slug is fixed.

FieldTypeNotes
titletext, required
messagerichText, requiredLexical (or any rich text editor); the default push body is a plain-text excerpt
recipientrelationship to usersOptional
audienceselect: user | broadcastLeft out on create, it is broadcast without a recipient and user with one. A broadcast cannot have a recipient
channelselectOne of the configured channel ids
isRead / readAtcheckbox / dateFor broadcasts, reads by a signed-in user return that user's own read state

Timestamps are on.

notification-reads

One row per user and broadcast they have read, since a broadcast is a single row shared by every user. Users can read and delete their own rows and create rows only for themselves and broadcasts they can see; the read endpoints below do this for them.

channel-subscriptions

One row per user and channel they made a choice for: user, channel, subscribed. See Channel subscriptions. Users can read, create, update and delete their own rows (a row a non-admin writes always becomes their own); admins can manage all rows.

push-subscriptions

Always registered, even without web push, so generated types and the database schema do not depend on VAPID keys. Without webPush.enabled it stays empty and only admins can access it. See WEBPUSH.md.

Default access

OperationAdmins (role === 'admin')Other signed-in users
readalltheir own (recipient is them) and broadcasts
create, deleteyesno
updateall fieldsisRead/readAt and the recipientEditableFields of their own personal notifications

Anonymous requests get nothing. The local API bypasses access control by default, so server code can create notifications freely. A personal notification whose recipient no longer exists stays audience: 'user' and is visible only to admins; an empty recipient is never treated as a broadcast.

Deleting a user through Payload also deletes their personal notifications, read receipts, channel subscriptions and push subscriptions. Deleting a notification deletes its read receipts.

Creating and reading notifications

// Personal notification
await payload.create({
  collection: 'notifications',
  data: {
    title: 'Order shipped',
    message: {
      root: {
        type: 'root',
        children: [{ type: 'paragraph', children: [{ type: 'text', text: 'Your order is on its way.' }] }],
      },
    },
    recipient: userId,
    channel: 'orders',
  },
})

// What a user can see, with broadcast read state filled in for them
const { docs } = await payload.find({
  collection: 'notifications',
  overrideAccess: false,
  user,
  sort: '-createdAt',
})

Read state endpoints

  • POST /api/notifications/:id/read marks a notification read for the signed-in user.
  • DELETE /api/notifications/:id/read marks it unread.

Both answer { id, isRead, readAt }. For a personal notification they set isRead/readAt on the notification (only its recipient may); for a broadcast they create or remove that user's notification-reads row, so other users are unaffected. Members cannot PATCH a broadcast.

When a signed-in user finds notifications (REST, GraphQL, or the local API with user), isRead/readAt on broadcasts are filled in from that user's receipts after the query. So where: { isRead: ... } filters broadcasts on the stored flag, not the user's state; query notification-reads for that.

Channel subscriptions

Each user can opt out of (and back into) each configured channel. A user with no stored choice for a channel is subscribed to it, so existing users keep receiving everything until they opt out. A notification without a channel is never affected.

An opt-out is honoured in two places:

  • Web push: the default delivery (autoPush to the notification's recipient, and WebPushManager.sendToRecipient) sends nothing to a recipient who opted out of the notification's channel. This comes on top of the per-device channels of a push subscription, which keeps working as before; see WEBPUSH.md.
  • Inbox queries: subscribedChannelsWhere returns a where clause that leaves out the user's opted-out channels, for personal notifications and broadcasts alike.

The notification document is always stored, so re-subscribing shows earlier notifications again. Opt-outs are a preference filter, not access control: a user can still read an opted-out notification they have access to by querying without the clause.

REST

All three require a signed-in user and only ever act on that user.

  • GET /api/channel-subscriptions/me answers { channels: [{ id, name, description?, subscribed }] } with every configured channel.
  • POST /api/channel-subscriptions/me/:channel subscribes; answers { channel, subscribed: true }.
  • DELETE /api/channel-subscriptions/me/:channel opts out; answers { channel, subscribed: false }.

An unknown channel id answers 404. Admins can manage other users' rows through the collection's standard REST/GraphQL API or the admin panel.

Server helpers

import {
  listChannelSubscriptions,
  isSubscribedToChannel,
  subscribeToChannel,
  unsubscribeFromChannel,
  subscribedChannelsWhere,
} from '@xtr-dev/payload-notifications'

await unsubscribeFromChannel({ payload, user, channel: 'marketing' })
await subscribeToChannel({ payload, user, channel: 'marketing' })
await isSubscribedToChannel({ payload, user, channel: 'marketing' }) // true
await listChannelSubscriptions({ payload, user }) // [{ id, name, description?, subscribed }, ...]

// Inbox: what the user may see, minus the channels they opted out of
const { docs } = await payload.find({
  collection: 'notifications',
  overrideAccess: false,
  user,
  where: await subscribedChannelsWhere({ payload, user }),
  sort: '-createdAt',
})

user is a user document or id. The helpers do not check access (they are for server code); pass req to run inside the request's transaction. setChannelSubscription({ ..., subscribed }) and getUnsubscribedChannels are exported too. Subscribing to an unknown channel throws UnknownChannelError.

If you deliver through other means (email, a custom webPush.findSubscriptions), call isSubscribedToChannel yourself before sending; the plugin cannot know who your custom targets are.

Customizing collections

Spread the existing config and hooks so the plugin's own behaviour (audience derivation, read state, auto push) stays in place. Example: send an email when a notification is created.

notificationsPlugin({
  channels: [{ id: 'default', name: 'Default' }],
  collectionOverrides: {
    notifications: (config) => ({
      ...config,
      hooks: {
        ...config.hooks,
        afterChange: [
          ...(config.hooks?.afterChange ?? []),
          async ({ doc, operation, req }) => {
            if (operation !== 'create' || !doc.recipient) return
            const id = typeof doc.recipient === 'object' ? doc.recipient.id : doc.recipient
            const user = await req.payload.findByID({ collection: 'users', id, req })
            // Don't throw here, or the notification create fails with the email
            await sendEmail({ to: user.email, subject: doc.title }).catch((err) => req.payload.logger.error(err))
          },
        ],
      },
    }),
  },
})

Recipient-editable fields

A recipient can update only isRead and readAt on their own notification. Every other field is admin-only for updates, including fields you add through collectionOverrides.notifications. List the fields recipients may change in recipientEditableFields:

notificationsPlugin({
  channels: [{ id: 'default', name: 'Default' }],
  collectionOverrides: {
    notifications: (config) => ({
      ...config,
      fields: [
        ...config.fields,
        { name: 'archived', type: 'checkbox' },
        { name: 'priority', type: 'text' }, // not listed below: admin-only
        {
          name: 'preferences',
          type: 'group',
          fields: [
            { name: 'pinned', type: 'checkbox' },
            { name: 'internalNote', type: 'text' }, // admin-only
          ],
        },
      ],
    }),
  },
  recipientEditableFields: ['archived', 'preferences.pinned'],
})
  • Use dots for nested fields (groups, named tabs, arrays, blocks). A path covers everything below it, so 'preferences' opens the whole group. Listing a field inside an array or blocks field also lets recipients add and remove its rows.
  • The rule is field-level access.update, so it applies to REST, GraphQL and the Local API with overrideAccess: false. As with any Payload field access, a change to a field the user may not update is dropped without an error; the rest of the update goes through.
  • A field that is not listed keeps its own access.update if it has one, and an admin must pass that too. A listed field is left exactly as you defined it.
  • recipient and audience cannot be listed, and a path that matches no field throws when the config is built.
  • Recipients still only reach their own personal notifications; this option does not widen which documents they can update.

Exports

PathExports
@xtr-dev/payload-notificationsnotificationsPlugin (also default), the channel subscription helpers, types NotificationsPluginOptions, NotificationChannel, NotificationAudience, PushNotificationContent, WebPushConfig
@xtr-dev/payload-notifications/clientClientPushManager, usePushNotifications, serviceWorkerCode, type PushSubscriptionData
@xtr-dev/payload-notifications/rscWebPushManager, createPushNotificationEndpoints, createPushSubscriptionsCollection, types WebPushConfig, PushSubscription, NotificationsPluginOptions
@xtr-dev/payload-notifications/sw.jsThe ready-to-serve service worker file

Upgrade notes

0.3.0

  • Schema: there is a new channel-subscriptions collection. On Postgres/SQLite, create and run a migration, and run payload generate:types. No data needs backfilling: users without rows are subscribed to every channel, as before.

  • Push: a recipient who opts out of a channel no longer gets default pushes for it. Nobody is opted out until they (or you) store an opt-out.

  • Breaking: fields added through collectionOverrides.notifications are admin-only for updates. Before, a recipient could update any such field on their own notification unless it had its own access.update. Now recipients can update only isRead/readAt and the paths in recipientEditableFields; other changes they send are dropped. To keep a field editable by recipients, list it: recipientEditableFields: ['archived']. A field with its own access.update that non-admins should pass must be listed too (its own rule then applies unchanged). No schema change.

0.2.0

  • Schema: notifications gains an audience column and there is a new notification-reads collection. On Postgres/SQLite, create and run a migration (payload migrate:create, then payload migrate). Run payload generate:types.
  • Backfill audience: rows with an empty audience are visible only to admins. Review rows without a recipient first (some may belong to users who were already deleted), then:
    UPDATE notifications SET audience = 'user' WHERE recipient_id IS NOT NULL;
    UPDATE notifications SET audience = 'broadcast' WHERE recipient_id IS NULL AND audience IS NULL;
    
    On MongoDB use the equivalent updateMany calls.
  • Stricter access: members can no longer create notifications, read other users' notifications, or update broadcasts. Use POST /api/notifications/:id/read instead of PATCH to mark notifications read. To keep the old behaviour, override access via collectionOverrides.notifications.
  • Notifications targeted through findSubscriptions without a recipient are created as broadcasts. Pass audience: 'user' to make them admin-only.
  • Push: default pushes now link to webPush.notificationUrl (default /) and set no icon, badge or actions. See WEBPUSH.md.

0.1.0

  • push-subscriptions is always registered. On Postgres/SQLite, if you never enabled web push, migrate to create its tables. It is admin-only while web push is disabled.
  • Members may only update isRead/readAt of their own notifications.

License

MIT

Similar plugins