Prompt
How do I connect push notifications to user actions in my app backend?
Latest observation
Nov 4, 2026
OpenAI APIWeb search: off
To connect push notifications to user actions in your backend, the usual pattern is:
-
Detect a user action
Example: user places an order, gets a message, is mentioned, etc. -
Create an event in your backend
- When the action happens, your backend should emit a domain event or call a notification service.
- Example event:
order.created,comment.replied,friend.requested.
-
Determine recipients
- Look up which users should receive the notification.
- Consider permissions, preferences, muted threads, device status, etc.
-
Persist the notification
- Store a notification record in your database so it can appear in an in-app notification center.
- This also helps with retries, auditing, and unread counts.
-
Send push via a push provider
- Use APNs for iOS, FCM for Android/Web, or a service like OneSignal, Expo Push, SNS, etc.
- Your backend sends a push payload to the provider with the user/device token.
-
Handle delivery and retries
- Queue the send job asynchronously so the user action response is fast.
- Retry failed sends, remove invalid tokens, and log outcomes.
Typical architecture
User action → API/backend endpoint → event/queue → notification worker → DB + push provider → device
Example flow:
- User comments on a post
- Backend saves the comment
- Backend publishes
comment.created - Notification worker finds post owner and mention targets
- Worker inserts notification rows
- Worker sends push notifications to the user’s active devices
What you need in your backend
1. Device token storage
Save push tokens per user/device.
Example table:
user_devices (
id,
user_id,
platform, -- ios, android, web
push_token,
enabled,
last_seen_at,
created_at
)
2. Notification table
Store notifications for in-app display.
notifications (
id,
user_id,
type,
title,
body,
data_json,
read_at,
created_at
)
3. Event/job queue
Use a queue so push sending is asynchronous:
- Redis + BullMQ / Sidekiq / Celery / RabbitMQ / SQS
4. Notification service
A dedicated service or module that:
- receives event info
- resolves recipients
- creates DB records
- sends push payloads
- tracks failures
Example payload
{
"title": "New comment",
"body": "Alex replied to your post",
"data": {
"type": "comment.created",
"postId": "123",
"commentId": "456"
}
}
The data field should let the app open the correct screen.
Best practices
- Do not send push directly in the request thread
- Use background jobs for reliability and speed.
- Respect notification preferences
- Let users opt out by type.
- Deduplicate
- Avoid sending multiple pushes for the same event.
- Batch if needed
- For high-volume systems, group notifications.
- Expire invalid tokens
- Remove tokens returned as unregistered/invalid.
- Keep push payloads small
- Send only enough info for the app to fetch details.
- Use deep links
- Push should navigate the user to the relevant page.
Simple pseudo-flow
def create_comment(user_id, post_id, text):
comment = save_comment(user_id, post_id, text)
publish_event("comment.created", {
"comment_id": comment.id,
"post_id": post_id,
"actor_user_id": user_id
})
return comment
Background worker:
def handle_comment_created(event):
recipients = get_post_owner_and_mentions(event["post_id"], event["actor_user_id"])
for recipient in recipients:
notification = create_notification(
user_id=recipient.id,
type="comment.created",
title="New comment",
body="Someone replied to your post",
data=event
)
send_push_to_user(recipient.id, notification)
If you want the shortest implementation path
- Store device tokens
- Save notification records
- Trigger on backend events
- Process in a queue/worker
- Send through FCM/APNs (or a unified provider)
If you want, I can also give you:
- a Node.js/Express example,
- a Python/Django example, or
- a database schema + worker design for your stack.