1.2.6 • Published 6 months ago

push-notification-service v1.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Push Notification Service

A comprehensive push notification package that provides both client and server functionality, enabling web developers to integrate push notifications into their projects seamlessly.

Features

  • Client and Server: Includes both client-side and server-side implementations.
  • Secure Communication: Uses VAPID keys for secure messaging.
  • Customizable Notifications: Supports custom titles, body, icons, and actions.
  • Persistent notification: Notifications will be delivered even if the app is not open, as long as the browser is running.
  • Express Integration: Easily set up routes for handling push notifications

Installation

Install the package via npm:

npm install push-notification-service

Or with Yarn:

yarn add push-notification-service

Server Usage

The PushNotificationServer is used to handle push notification subscriptions and send notifications from your backend.

import express from "express";
import { PushNotificationServer } from "push-notification-service";
import cors from "cors";

const app = express();
app.use(express.json());

app.use(
  cors({
    origin: "*", // Adjust origin based on your environment
  })
);

const pushServer = new PushNotificationServer({
  publicKey: "YOUR_PUBLIC_VAPID_KEY",
  privateKey: "YOUR_PRIVATE_VAPID_KEY",
  email: "your-email@example.com", // Used for VAPID authentication
});

// Setup server routes for push notifications
pushServer.setupRoutes(app);

app.listen(8080, () => console.log("Server running on port 8080"));

Configuration Options for PushNotificationServer

OptionTypeDescription
publicKeystringThe public VAPID key for your push notifications.
privateKeystringThe private VAPID key for your push notifications.
emailstringA contact email for VAPID authentication.

Client Usage

The PushNotificationClient is used to initialize and send notifications from the frontend.

import { PushNotificationClient } from "push-notification-service";

const pushClient = new PushNotificationClient({
  serverUrl: "http://localhost:8080", // Replace with your server URL
  publicVapidKey: "YOUR_PUBLIC_VAPID_KEY", // Replace with your VAPID public key
  userId: "unique-user-id", // Replace with the user's unique identifier
});

// Initialize the push notification client
async function initializeNotifications() {
  try {
    await pushClient.initialize();
    console.log("Push notifications initialized successfully");
  } catch (error) {
    console.error("Failed to initialize push notifications:", error.message);
  }
}

initializeNotifications();

Sending Notification

function handleSendNotification() {
  try {
    pushClient.sendNotification({
      body: "This is a sample notification!",
      icon: "https://example.com/icon.png",
      image: "https://example.com/image.png",
      badge: "https://example.com/badge.png",
      url: "https://example.com",
    });
  } catch (error) {
    console.error("Failed to send notification:", error.message);
  }
}

handleSendNotification();

Configuration Options for PushNotificationClient

OptionTypeDescription
serverUrlstringThe URL of your push notification server.
publicVapidKeystringThe private VAPID key for your push notifications.
userIDstringA unique identifier for the user (e.g., email, username, or ID).

Requirement

  • A backend server running PushNotificationServer with VAPID keys.
  • A modern browser that supports the Push API.
  • HTTPS-enabled site (required for Push API in production).

Contributing

We welcome contributions! Please follow these steps to contribute:

  • Fork the repository.
  • Create a new branch for your feature or bug fix.
  • Submit a pull request with a detailed description of your changes
1.2.6

6 months ago

1.2.5

6 months ago

1.2.4

6 months ago

1.2.3

6 months ago

1.2.2

6 months ago

1.2.1

6 months ago

1.2.0

6 months ago

1.1.9

6 months ago

1.1.8

6 months ago

1.1.7

6 months ago

1.1.6

6 months ago

1.1.5

6 months ago

1.1.4

6 months ago

1.1.3

6 months ago

1.1.2

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago