Prompt
send push notifications from backend
Latest observation
Oct 10, 2026
OpenAI APIWeb search: off
To send push notifications from a backend, you generally use a push provider or platform-specific APIs.
Common options
1) Web Push
For browsers, use:
- Service Workers
- Push API
- VAPID keys
- A backend library like
web-push
Example flow:
- Frontend asks permission and subscribes the device/browser.
- Frontend sends the subscription object to your backend.
- Backend stores subscriptions.
- Backend sends push payloads to those subscriptions.
2) Mobile push notifications
For iOS/Android, most backends use a push service:
- Firebase Cloud Messaging (FCM) for Android, web, and iOS
- Apple Push Notification service (APNs) for iOS
- Or a provider like OneSignal, Expo, etc.
Typical flow:
- App gets a device push token.
- App sends token to backend.
- Backend stores token.
- Backend sends notification via FCM/APNs/provider API.
Example: sending via FCM from backend
If you're using Firebase Cloud Messaging, your backend can call FCM with the device token.
Node.js example
import admin from "firebase-admin";
import serviceAccount from "./serviceAccountKey.json" assert { type: "json" };
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
async function sendNotification(token) {
const message = {
token,
notification: {
title: "Hello",
body: "You have a new message",
},
data: {
type: "chat",
chatId: "123",
},
};
const response = await admin.messaging().send(message);
console.log("Sent:", response);
}
Python example
import firebase_admin
from firebase_admin import credentials, messaging
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
message = messaging.Message(
notification=messaging.Notification(
title="Hello",
body="You have a new message",
),
token="DEVICE_TOKEN_HERE",
)
response = messaging.send(message)
print("Sent:", response)
Best practices
- Store and refresh device tokens.
- Remove invalid/expired tokens.
- Send notifications asynchronously from a job queue.
- Keep payloads small.
- Use topic messaging or segments if you need to target many users.
If you want, I can show you:
- a Node.js backend example
- a Python backend example
- FCM setup steps
- iOS/Android token handling
- web push with service workers