Prompt
How do I trigger push notifications from a webhook or event?
Latest observation
To trigger push notifications from a webhook or event, you generally need this flow:
-
An event happens
Example: a payment succeeds, a new message arrives, a ticket is updated. -
A webhook endpoint or event handler receives it
Your backend exposes an endpoint likePOST /webhooks/...or listens to events from a queue / pub-sub system. -
Your backend decides who should be notified
Look up the user(s), device tokens, topics, or segments. -
Your backend calls a push notification service
Common options:- Firebase Cloud Messaging (FCM)
- Apple Push Notification service (APNs) directly for iOS
- OneSignal
- Expo Notifications
- AWS SNS / Pinpoint
- Pusher Beams, etc.
-
The service delivers the notification to the device(s)
Typical architecture
Webhook/Event → Your server → Push provider API → Mobile device
You usually do not send push notifications directly from the webhook provider. Instead, your webhook triggers your own backend, and your backend sends the push.
Example with a webhook
1. Receive webhook
app.post('/webhook', async (req, res) => {
const event = req.body;
if (event.type === 'order.shipped') {
// find user/device tokens
await sendPushToUser(event.userId, {
title: 'Your order shipped!',
body: `Tracking: ${event.trackingNumber}`,
});
}
res.sendStatus(200);
});
2. Send push via FCM
async function sendPushToUser(userId, notification) {
const tokens = await getDeviceTokensForUser(userId);
await fetch('https://fcm.googleapis.com/fcm/send', {
method: 'POST',
headers: {
'Authorization': `key=${process.env.FCM_SERVER_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
registration_ids: tokens,
notification,
}),
});
}
Best practices
- Verify webhook signatures so only trusted events trigger pushes.
- Make webhook handling fast; queue the notification job if possible.
- Deduplicate events to avoid duplicate notifications.
- Store device tokens per user and clean up invalid ones.
- Use background jobs for retries and reliability.
- Respect user preferences and notification settings.
Recommended pattern for reliability
For production, do this:
Webhook handler
- validates event
- stores event in DB / queue
- returns
200 OKquickly
Worker
- processes event
- determines recipients
- sends push
- retries on failure
If you want, I can show you a complete example for:
- Node.js + Firebase Cloud Messaging
- Python + FCM
- OneSignal
- iOS/Android app setup
- or a webhook-to-push architecture diagram