- Sign up at
/sign-up and create a project. We generate a VAPID key pair for you and assign a per-project API key. - Install the SDK in your Next.js / React app:
pnpm add @piro0919/next-push
- Drop in the
usePush hook with the values from your project's "SDK setup" card:'use client';
import { usePush } from "@piro0919/next-push";
export function Subscribe() {
const { subscribe } = usePush({
apiBase: "https://nesh.kkweb.io/api/v1/projects/<projectId>",
vapidPublicKey: "<VAPID public key>",
// Optional — scope subscriptions to your application's user id
// so you can target sends by user from the dashboard / REST API.
userId: currentUser.id,
});
return <button onClick={subscribe}>Enable notifications</button>;
}
- Add a service worker at
public/sw.js. The default handlers shipped with @piro0919/next-push/sw render notifications and fire shown / click tracking automatically:import { handlePush, handleClick } from "@piro0919/next-push/sw";
self.addEventListener("push", (event) => handlePush(event));
self.addEventListener("notificationclick", (event) => handleClick(event));
- Send your first notification from the dashboard, or via the REST API.