This is a library for declarative use of Notification API with Angular.
npm i @ng-web-apis/notification
- Import the
NotificationService
into your Angular component or service where you want to use it.
import {NotificationService} from '@ng-web-apis/notification';
- Inject the
NotificationService
into your component's constructor or withinject
(Angular 14+).
// in constructor
constructor(private notificationAPIService: NotificationService) {}
// via inject
notificationAPIService = inject(NotificationService);
- Use the
requestPermission
andopen
methods to request permission and open a notification.
this.notificationAPIService
.requestPermission()
.pipe(
filter((permission) => permission === 'granted'),
switchMap(() =>
this.notificationAPIService.open('Hello world!', {
body: 'This is a notification',
requireInteraction: true,
}),
),
)
.subscribe();
You can try online demo here
Other Web APIs for Angular by @ng-web-apis