Prompt
How do I add push notifications and user targeting to a mobile backend service?
Latest observation
To add push notifications and user targeting to a mobile backend service, you typically need three pieces:
- A push provider to deliver notifications to devices
- A device-token registry in your backend
- Targeting logic to decide which users/devices receive which messages
Here’s a practical blueprint.
1) Pick a push notification service
Most mobile backends use one of these:
- Firebase Cloud Messaging (FCM) — common for Android and iOS
- Apple Push Notification service (APNs) — required for iOS delivery directly
- OneSignal / Braze / Airship — higher-level platforms with segmentation and analytics
- Amazon SNS / Azure Notification Hubs — cloud-integrated options
Typical recommendation:
- Use FCM for Android
- For iOS, either:
- send through FCM (which routes to APNs), or
- integrate APNs directly
2) Store push tokens for each device
When the app starts or the user logs in, it gets a device token from the OS/push provider and sends it to your backend.
Backend should store:
user_iddevice_idor installation IDplatform(ios,android)push_tokenapp_versionlocalelast_seen- optionally
tags/segments
Example schema
devices (
id,
user_id,
platform,
push_token,
device_id,
created_at,
updated_at,
last_seen_at
)
Important
- Tokens can change; update them whenever the app refreshes them.
- Remove invalid tokens when push providers say they are expired/unregistered.
3) Build an API for token registration
Your app should call an endpoint like:
POST /api/devices/register
Payload:
{
"device_id": "abc123",
"platform": "ios",
"push_token": "token-here",
"app_version": "1.4.2",
"locale": "en-US"
}
If the user is authenticated, associate the device with their user account.
If the user is anonymous, you can:
- store it against a temporary installation record
- later attach it when the user logs in
4) Send push notifications from your backend
Your backend should have a notification service/module that:
- determines the target audience
- fetches their device tokens
- sends notifications through the push provider
- handles errors and retries
Example flow
- An event occurs: order shipped, new message, promo, reminder
- Backend checks targeting rules
- Backend looks up matching users/devices
- Backend sends notification payloads to push provider
- Backend records delivery attempt/status
5) Add user targeting
User targeting means sending notifications only to relevant users. Common targeting methods:
A. By user ID
Send to one specific user.
Example:
- “Your password was changed”
- “Your order has shipped”
B. By tags/attributes
Store attributes like:
- location
- language
- subscription tier
- interests
- last active date
Then target users like:
- premium users
- users in Canada
- users who like sports
C. By segments
Predefined groups built from rules:
- “Inactive for 7 days”
- “Purchased in the last 30 days”
- “iOS users in Europe”
D. By event trigger
Trigger based on app/business events:
- abandoned cart
- new friend request
- new content available
6) Use topics or groups where appropriate
Some push services support server-side group targeting.
FCM topics
Users/devices can subscribe to topics like:
newssportspromo_us
Then your backend sends one notification to a topic instead of individual tokens.
Good for:
- broadcast messages
- broad interest categories
Less good for:
- highly personalized notifications
7) Create a notification queue/worker
Don’t send pushes inline in your API request if the audience is large.
Use:
- a job queue
- background workers
- retries with backoff
- dead-letter handling
Why
- avoids slowing user-facing requests
- helps with rate limits
- improves reliability
- supports batching
8) Track delivery and engagement
Store:
- sent time
- provider response
- success/failure
- opened/clicked if supported
- notification type/campaign ID
This helps with:
- debugging failed sends
- measuring CTR/open rate
- cleaning invalid tokens
- improving targeting
9) Handle token lifecycle correctly
Your backend should:
- update tokens when they change
- delete tokens when invalid
- deduplicate tokens
- support multiple devices per user
A single user may have:
- phone
- tablet
- multiple app installs
So target by user, then fan out to all active devices.
10) Secure and throttle notification sending
Add protections:
- authenticate who can create campaigns
- rate limit send endpoints
- prevent spam
- validate notification content
- avoid sending sensitive data in push payloads
For privacy, keep payloads minimal:
- “You have a new message” instead of
- “Bob said: ‘Here’s my SSN…’”
Example architecture
Mobile App
-> registers device token
-> subscribes to topics / sends preferences
Backend API
-> stores device tokens and user attributes
-> receives business events
-> creates notification jobs
Worker / Queue
-> resolves targeting
-> sends to FCM/APNs
-> stores results
Push Provider
-> delivers to OS
-> app receives notification
Minimal implementation plan
If you want the fastest path:
- Integrate FCM
- Add a
devicestable - Add
POST /devices/register - Store tokens per user
- Build a notification worker
- Implement targeting by:
- user ID
- tags
- simple segments
- Add token cleanup and analytics
Recommended data model additions
users- profile attributes
- preferences
- opt-in status
devices- push token and device info
notification_campaigns- title/body/data/targeting rules
notification_deliveries- per-device send status
user_segments- computed segment membership
Key best practices
- Let users opt out of marketing pushes
- Separate transactional and promotional notifications
- Personalize content by language/time zone
- Use background jobs for sending
- Clean up invalid tokens regularly
- Test on real devices
- Keep payloads small
- Respect platform rules and quiet hours
If you want, I can also give you:
- a sample database schema,
- backend API endpoints, or
- a code example in Node.js, Python, Java, or Go.