A notification system keeps users informed. Here is how to build one with bell icon, dropdown, and real-time updates.
Step 1: Database Schema
// db/schema.ts (Drizzle ORM)
import { pgTable, text, timestamp, boolean, uuid } from "drizzle-orm/pg-core";
export const notifications = pgTable("notifications", {
id: uuid("id").defaultRandom().primaryKey(),
userId: text("user_id").notNull(),
title: text("title").notNull(),
message: text("message").notNull(),
type: text("type").$type<"info" | "success" | "warning" | "error">().default("info"),
link: text("link"),
read: boolean("read").default(false),
createdAt: timestamp("created_at").defaultNow(),
});
Step 2: API Routes
// app/api/notifications/route.ts
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { db } from "@/db";
import { notifications } from "@/db/schema";
import { eq, desc } from "drizzle-orm";
export async function GET() {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const items = await db
.select()
.from(notifications)
.where(eq(notifications.userId, session.user.id))
.orderBy(desc(notifications.createdAt))
.limit(50);
return NextResponse.json(items);
}
// app/api/notifications/read/route.ts
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { db } from "@/db";
import { notifications } from "@/db/schema";
import { eq, and } from "drizzle-orm";
export async function PATCH(req: Request) {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { id } = await req.json();
await db
.update(notifications)
.set({ read: true })
.where(
and(
eq(notifications.id, id),
eq(notifications.userId, session.user.id)
)
);
return NextResponse.json({ success: true });
}
// Mark all as read
export async function PUT() {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
await db
.update(notifications)
.set({ read: true })
.where(eq(notifications.userId, session.user.id));
return NextResponse.json({ success: true });
}
Step 3: Notification Bell Component
"use client";
import { useState, useEffect, useRef } from "react";
import { Bell } from "lucide-react";
interface Notification {
id: string;
title: string;
message: string;
type: "info" | "success" | "warning" | "error";
link?: string;
read: boolean;
createdAt: string;
}
export function NotificationBell() {
const [notifications, setNotifications] = useState<Notification[]>([]);
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const unreadCount = notifications.filter((n) => !n.read).length;
useEffect(() => {
fetchNotifications();
const interval = setInterval(fetchNotifications, 30000); // Poll every 30s
return () => clearInterval(interval);
}, []);
// Close on outside click
useEffect(() => {
function handleClickOutside(e: MouseEvent) {
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
setIsOpen(false);
}
}
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
async function fetchNotifications() {
const res = await fetch("/api/notifications");
if (res.ok) {
setNotifications(await res.json());
}
}
async function markAsRead(id: string) {
await fetch("/api/notifications/read", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id }),
});
setNotifications((prev) =>
prev.map((n) => (n.id === id ? { ...n, read: true } : n))
);
}
async function markAllAsRead() {
await fetch("/api/notifications/read", { method: "PUT" });
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
}
function timeAgo(dateStr: string) {
const seconds = Math.floor(
(Date.now() - new Date(dateStr).getTime()) / 1000
);
if (seconds < 60) return "just now";
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
return `${Math.floor(seconds / 86400)}d ago`;
}
return (
<div className="relative" ref={dropdownRef}>
<button
onClick={() => setIsOpen(!isOpen)}
className="relative rounded-full p-2 hover:bg-gray-100 dark:hover:bg-gray-800"
aria-label="Notifications"
>
<Bell className="h-5 w-5" />
{unreadCount > 0 && (
<span className="absolute -right-0.5 -top-0.5 flex h-4 w-4 items-center justify-center rounded-full bg-red-500 text-[10px] font-bold text-white">
{unreadCount > 9 ? "9+" : unreadCount}
</span>
)}
</button>
{isOpen && (
<div className="absolute right-0 top-full z-50 mt-2 w-80 rounded-xl border bg-white shadow-xl dark:border-gray-700 dark:bg-gray-900">
<div className="flex items-center justify-between border-b p-3 dark:border-gray-700">
<h3 className="font-semibold">Notifications</h3>
{unreadCount > 0 && (
<button
onClick={markAllAsRead}
className="text-xs text-blue-600 hover:underline"
>
Mark all as read
</button>
)}
</div>
<div className="max-h-96 overflow-y-auto">
{notifications.length === 0 ? (
<p className="p-4 text-center text-sm text-gray-500">
No notifications yet
</p>
) : (
notifications.map((notification) => (
<div
key={notification.id}
onClick={() => markAsRead(notification.id)}
className={`cursor-pointer border-b p-3 transition-colors hover:bg-gray-50 dark:border-gray-800 dark:hover:bg-gray-800 ${
!notification.read
? "bg-blue-50 dark:bg-blue-950/20"
: ""
}`}
>
<div className="flex items-start gap-3">
{!notification.read && (
<span className="mt-1.5 h-2 w-2 shrink-0 rounded-full bg-blue-500" />
)}
<div className="flex-1">
<p className="text-sm font-medium">
{notification.title}
</p>
<p className="text-xs text-gray-500">
{notification.message}
</p>
<p className="mt-1 text-[10px] text-gray-400">
{timeAgo(notification.createdAt)}
</p>
</div>
</div>
</div>
))
)}
</div>
</div>
)}
</div>
);
}
Step 4: Server-Sent Events for Real-Time Updates
// app/api/notifications/stream/route.ts
import { auth } from "@/lib/auth";
export async function GET() {
const session = await auth();
if (!session?.user?.id) {
return new Response("Unauthorized", { status: 401 });
}
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
// Send heartbeat every 15 seconds
const heartbeat = setInterval(() => {
controller.enqueue(encoder.encode(": heartbeat\n\n"));
}, 15000);
// Subscribe to notifications for this user
const unsubscribe = subscribeToNotifications(
session.user.id,
(notification) => {
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(notification)}\n\n`)
);
}
);
// Cleanup
return () => {
clearInterval(heartbeat);
unsubscribe();
};
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
});
}
Need a Notification System?
We build real-time web applications with notification systems, live updates, and user engagement features. Contact us to discuss your project.