Notifications-library is a small and simple library in TypeScript to manage a notifications system
Create an instance of the NotificationCenter
const notificationCenterObject = new NotificationCenter;
If you want to use a remote system, set the configuration
const configuration = { fetchUrl: 'anyValidURL1',
createUrl: 'anyValidURL2',
updateUrl:'anyValidURL3' };
notificationCenterObject.setConfig(configuration);
otherwise you can have a full local system.
You can set the sender of the notifications
const sender = 'AnyValidSender';
notificationCenterObject.setSender(sender);
There are different methods offered by the NotificationCenter class
This method allow the user to get the notifications list
await notificationCenterObject.getAllNotifications();
This method allow the user to get a specific notification by passing its ID as argument
// notification.id is the id of the notification you want to get
const requestedNotification = await notificationCenterObject.getNotificationByID(notification.id);
This method allow the user to send a notification
const notification = {
title: 'There is a new notification',
message: 'Hello, im the first notification!'
};
await notificationCenterObject.sendNotification(notification);
This method allow the user to mark a specific notification by passing its ID as argument
// notification.id is the id of the notification you want to mark as read
await notificationCenterObject.markNotificationAsRead(notification.id);
This method allow the user to mark all the notifications as read
await notificationCenterObject.markAllAsRRead();
This method allow the user to delete a specific notification by passing its ID as argument
// notification.id is the id of the notification you want to delete
await notificationCenterObject.deleteNotificationByID(notification.id);
This method allow the user to delete all the notifications
await notificationCenterObject.deleteAllNotifications();
This method allow the user to subscribe to the NotificationCenter and get notify each time a new notification is sent or read
// This function will be called each time a new notification is sent or read
function onNewNotification(notification: Notification) {
console.log(notification);
}
// onNewNotification is subscribed
notificationCenterObject.addSubscriber(onNewNotification);