1.0.0 âĒ Published 7 months ago
rm-pushnotify v1.0.0
ð PushNotification Library
The PushNotification Library is a lightweight, easy-to-use JavaScript library that simplifies the process of managing browser notifications and service workers. Whether you're a beginner or an experienced developer, this library makes it straightforward to integrate notifications into your web application.
âĻ Features
- ðïļ Singleton Design Pattern: Ensures only one instance of the library is created.
- âïļ Service Worker Management: Register, unregister, and update service workers effortlessly.
- ð Easy Notification Display: Customize notifications with titles, icons, and more.
- ð Cross-Browser Compatibility: Works with modern browsers supporting Service Workers and Notifications API.
- ðŠķ Lightweight: Minimal setup and fast performance.
ðĶ Installation
Install the library via npm or yarn:
npm install rm-pushnotify
# or
yarn add rm-pushnotify
Live DEMO
ð Usage Guide
âĄïļ Import the Library
import PushNotification from 'rm-pushnotify';
âĄïļ Initialize the PushNotification Instance
const pushNotification = new PushNotification('/sw.js');
âĄïļ Request Notification Permission
await pushNotification.requestPermission();
âĄïļ Register the Service Worker
const registration = await pushNotification.registerServiceWorker();
âĄïļ Show Notifications
pushNotification.showNotification('Hello, World!', {
body: 'This is a test notification!',
icon: '/path-to-icon/icon.png',
});
âĄïļ Create a new file sw.js at root of project. Put below content
// src/sw.js
self.addEventListener('push', function (event) {
const data = event.data.json();
const options = {
body: data.body,
icon: 'icon.png',
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
self.addEventListener('notificationclick', function (event) {
const action = event.action;
if (action === 'open_app') {
event.waitUntil(
clients.openWindow('/') // Open your app or a specific URL
);
} else if (action === 'dismiss') {
event.notification.close(); // Close the notification
} else {
event.waitUntil(
clients.openWindow('/') // Fallback to opening the app
);
}
});
ðŧ Compatibility
This library is written in JavaScript and is compatible with:
- Frameworks: Angular, React, Vue.js, Vanilla JavaScript
- Node.js Versions: 14.x and above
- Package Managers: npm, yarn
ð Browser Support
Browser | Notifications API | Service Workers |
---|---|---|
Chrome | â Supported | â Supported |
Firefox | â Supported | â Supported |
Microsoft Edge | â Supported | â Supported |
Safari (macOS) | â Limited Support | â Not Supported |
Safari (iOS) | â Not Supported | â Not Supported |
Note: Ensure your application uses HTTPS, as service workers require a secure context.
ð Security Best Practices
- HTTPS Only: Always serve your application over HTTPS.
- Update Regularly: Keep your service worker updated to avoid vulnerabilities.
- No Sensitive Data: Avoid exposing sensitive data in your service worker.
ð Issues
If you encounter any issues:
- Search existing issues in the GitHub repository.
- Create a new issue with detailed steps to reproduce the problem.
ðĻâðŧ Author Services
Are you interested in this library but lacks features? Write to the author, he can do it for you.
ð Keywords
- Push Notifications
- JavaScript Library
- Service Workers
- Browser Notifications
- Notifications API
- Web Development
ð License
This library is licensed under the MIT License.
ðĪ Author
Rajat Malik
1.0.0
7 months ago