Skip to main content

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​

VariableDescription
PUSH_NOTIFICATION_PUBLIC_KEYVAPID public key for push service registration
PUSH_NOTIFICATION_PRIVATE_KEYVAPID 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​

ParameterTypeRequiredDescription
subscriptionObjectYesUser's push subscription object
subjectStringYesURL or mailto identifying your service
payloadStringYesStringified JSON with title and body
urgencyStringNovery-low, low, normal, or high
topicStringNoIdentifier 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​

PropertyValue
Keyshop.unchained.worker-plugin.push-notification
TypePUSH
Sourceworker/push-notification

External Resources​