Push Notification Worker
Send W3C compliant web push notifications to subscribed users, encrypted (RFC 8291) and signed (VAPID, RFC 8292) with the Web Crypto API, so no extra package is needed.
Included in All Preset
Registered automatically by registerAllPlugins().
Registration
import { pluginRegistry } from '@unchainedshop/core';
import { PushNotificationPlugin } from '@unchainedshop/plugins/worker/push-notification';
pluginRegistry.register(PushNotificationPlugin);
Environment Variables
| Variable | Description |
|---|---|
PUSH_NOTIFICATION_PUBLIC_KEY | VAPID public key for push service registration |
PUSH_NOTIFICATION_PRIVATE_KEY | VAPID private key for signing push messages |
Generating VAPID Keys
npx web-push generate-vapid-keys
Usage
Send Push Notification
mutation SendPush {
addWork(
type: PUSH
input: {
subscription: {
endpoint: "https://fcm.googleapis.com/..."
expirationTime: null
keys: {
auth: "auth-key"
p256dh: "p256dh-key"
}
}
subject: "https://yourshop.com"
payload: "{\"title\": \"Order Shipped\", \"body\": \"Your order is on its way!\"}"
urgency: "normal"
}
) {
_id
status
}
}
Input Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
subscription | Object | Yes | User's push subscription object |
subject | String | Yes | URL or mailto identifying your service |
payload | String | Yes | Stringified JSON with title and body |
urgency | String | No | very-low, low, normal, or high |
topic | String | No | Identifier for notification coalescing (max 32 chars) |
Subscription Object
{
"endpoint": "https://push-service-url...",
"expirationTime": null,
"keys": {
"auth": "authentication-secret",
"p256dh": "public-key"
}
}
GraphQL API
Get VAPID Public Key
The public key is exposed for client-side subscription:
query GetShopInfo {
shopInfo {
vapidPublicKey
}
}
Manage User Subscriptions
Add subscription:
mutation AddPushSubscription {
addPushSubscription(
subscription: {
endpoint: "https://fcm.googleapis.com/..."
expirationTime: null
keys: {
auth: "auth-key"
p256dh: "p256dh-key"
}
}
unsubscribeFromOtherUsers: true
) {
_id
pushSubscriptions {
_id
endpoint
userAgent
}
}
}
Remove subscription:
mutation RemovePushSubscription {
removePushSubscription(p256dh: "subscription-p256dh-key") {
_id
}
}
Note: unsubscribeFromOtherUsers: true removes this subscription from other users.
Query User Subscriptions
query GetMySubscriptions {
me {
pushSubscriptions {
_id # p256dh value
endpoint
expirationTime
userAgent
}
}
}
Template Example
Send push notifications via the messaging system:
import { MessagingDirector } from '@unchainedshop/core';
MessagingDirector.registerTemplate('ORDER_SHIPPED', async ({ orderId }, context) => {
const { modules } = context;
const order = await modules.orders.findOrder({ orderId });
const user = await modules.users.findUserById(order.userId);
const pushNotifications = (user?.pushSubscriptions || []).map((subscription) => ({
type: 'PUSH',
input: {
subscription,
subject: 'https://yourshop.com',
payload: JSON.stringify({
title: 'Order Shipped!',
body: `Your order ${order.orderNumber} is on its way`,
}),
},
}));
return pushNotifications;
});
Adapter Details
| Property | Value |
|---|---|
| Key | shop.unchained.worker-plugin.push-notification |
| Type | PUSH |
| Source | worker/push-notification |
External Resources
- W3C Push API Specification
- RFC 8291: Message Encryption for Web Push
- RFC 8292: VAPID
- Web Push Notifications Guide (MDN)