5 Real Examples of Implementing Push Notifications in Web Apps

Posted by

From reminders to real-time alerts, here’s how to use the Push API like a pro (without relying on Firebase or OneSignal).

From reminders to real-time alerts, here’s how to use the Push API like a pro (without relying on Firebase or OneSignal).

Introduction

Let’s be honest, push notifications are everywhere.
Slack pings you. Twitter alerts you. Even your favorite PWA reminds you to check your streak.

But here’s what most developers don’t realize:

You don’t need Firebase or third-party SDKs to build them.

Modern browsers have everything you need: Push API + Service Workers + VAPID keys to send notifications directly from your server to your users’ devices.

In this post, we’ll explore 5 practical, real-world examples of how to use push notifications effectively with concise code snippets you can adapt right now.


Example #1 New Message Notification (Chat App)

The classic use case: a chat app notifying the user when a new message arrives.

Frontend: Register + Subscribe

if ("serviceWorker" in navigator) {
const registration = await navigator.serviceWorker.register("/sw.js");

const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array("<YOUR_VAPID_PUBLIC_KEY>"),
});

await fetch("/subscribe", {
method: "POST",
body: JSON.stringify(subscription),
headers: { "Content-Type": "application/json" },
});
}

Service Worker (sw.js)

self.addEventListener("push", (event) => {
const data = event.data.json();
self.registration.showNotification("💬 New Message", {
body: `${data.sender}: ${data.text}`,
icon: "/chat-icon.png",
badge: "/badge.png",
});
});

Now, when your backend detects a new message, it sends a push to that user, and the notification shows up even if the tab is closed.

✅ Great for chat apps like Slack, Discord, or internal company messengers.


Example #2 Order Status Updates (E-Commerce)

Ever ordered something online and got a “Your package is out for delivery” notification?
That’s a perfect push notification scenario.

Backend (Node.js)

Using the web-push package:

import webpush from "web-push";

const payload = JSON.stringify({
title: "🛍️ Order Update",
body: "Your order #12345 is out for delivery!",
});
webpush.sendNotification(subscription, payload);

Service Worker

self.addEventListener("push", (event) => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: "/delivery.png",
});
});

✅ Keeps customers informed in real time.
✅ Works offline; users see it as soon as they reconnect.


Example #3 Scheduled Reminders (Productivity App)

Imagine a to-do or habit-tracking app sending a push like:

“✅ Time to review your daily goals!”

You can trigger that from your backend using cron jobs.

Server-Side (Example with Node Cron)

import cron from "node-cron";
import webpush from "web-push";

cron.schedule("0 9 * * *", () => {
const payload = JSON.stringify({
title: "⏰ Morning Reminder",
body: "Check your daily tasks!",
});
subscriptions.forEach((sub) => webpush.sendNotification(sub, payload));
});

Service Worker

self.addEventListener("push", (event) => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: "/reminder.png",
});
});

✅ No third-party scheduler needed.
✅ Reminders appear even if the user never opens the app that day.


Example #4 Breaking News Alerts (Content App)

If you run a news, blog, or content site, push notifications can instantly re-engage readers.

const payload = JSON.stringify({
title: "📰 Breaking News",
body: "JavaScript just got a new built-in Array method!",
data: { url: "/articles/new-array-method" },
});

In Service Worker:

self.addEventListener("push", (event) => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: "/news.png",
data: data.data,
});
});

self.addEventListener("notificationclick", (event) => {
event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data.url));
});

✅ Clicking the notification takes the user straight to the new article.
✅ Perfect for news, blogs, and newsletters.


Example #5 Real-Time System Alerts (Admin Dashboards)

Push notifications aren’t just for users; they’re great for admin dashboards too.

For example:

“⚠️ Server Load Exceeded 80%”
“✅ New user signed up: John Doe”

Backend

const payload = JSON.stringify({
title: "⚠️ System Alert",
body: "CPU usage exceeded 80% on Server #3",
});
webpush.sendNotification(adminSubscription, payload);

Service Worker

self.addEventListener("push", (event) => {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: "/alert.png",
requireInteraction: true, // keeps the notification visible
});
});

✅ Keeps admins informed in real time.
✅ No need to keep the dashboard open in a tab.


Bonus Tips for Smarter Push Notifications

🧩 Use meaningful titles: Users scan notifications fast.
💬 Keep messages short: 50–80 characters is the sweet spot.
🔔 Don’t spam: Too many notifications = instant unsubscription.
🌐 Group-related messages: Avoid sending 10 separate pushes for 10 small updates.


Quick Recap


Why This Matters

Push notifications are one of the most powerful user engagement tools on the web, and they’re 100% native.

✅ No SDKs
✅ No vendor lock-in
✅ Works across Chrome, Edge, Firefox, and Android

Once you master Service Workers and the Push API, your web apps stop being “just websites” they become real-time platforms.


Conclusion

With just a Service Worker and a few lines of backend code, you can build push systems for:

  • Messages
  • Alerts
  • Reminders
  • News
  • Monitoring

You don’t need Firebase or plugins; the browser gives you all the tools.

✅ Fast setup
✅ Full control
✅ Zero dependencies

The next time you want to re-engage users, skip the emails push it directly to their screens.


Call to Action

Which of these use cases fits your app best?
Drop a comment 👇 I’d love to see what kind of notifications you’re building.

And if your teammate still says, “Push notifications only work with Firebase”…
Send them this post and show them how wrong they are

Leave a Reply

Your email address will not be published. Required fields are marked *