You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Web Notifications API wrapper with permission management and tracking.
Quick Start
import{Result}from'@zappzarapp/browser-utils/core';import{BrowserNotification}from'@zappzarapp/browser-utils/notification';// Check support and request permissionif(BrowserNotification.isSupported()){constpermResult=awaitBrowserNotification.requestPermission();if(Result.isOk(permResult)&&permResult.value==='granted'){// Show notificationconstresult=awaitBrowserNotification.show('Hello!',{body: 'This is a notification',icon: '/icon.png',});}}
// NotificationError typesNotificationError.notSupported();// API not supportedNotificationError.permissionDenied();// Permission deniedNotificationError.showFailed(e);// Failed to show notification
Notification Options
interfaceNotificationOptions{body?: string;// Notification body texticon?: string;// Icon URLbadge?: string;// Badge URL (mobile)image?: string;// Image URLtag?: string;// Tag for grouping/replacingdata?: unknown;// Custom datarequireInteraction?: boolean;// Keep visible until dismissedsilent?: boolean;// Suppress sound/vibrationvibrate?: number[];// Vibration patternactions?: NotificationAction[];// Action buttons (Service Worker only)}
Usage Examples
Basic Notification
asyncfunctionnotifyUser(message: string): Promise<void>{if(!BrowserNotification.isSupported()){console.log('Notifications not supported');return;}constresult=awaitBrowserNotification.show('App Name',{body: message,icon: '/icons/notification.png',});if(Result.isErr(result)){console.error('Notification failed:',result.error);}}
Notification with Event Handlers
asyncfunctionshowInteractiveNotification(): Promise<void>{constresult=awaitBrowserNotification.showWithHandlers('New Message',{body: 'You have a new message from John',icon: '/icons/message.png',tag: 'new-message',onClick: (event)=>{event.preventDefault();window.focus();navigateTo('/messages');},onClose: ()=>{console.log('Notification closed');},onError: (event)=>{console.error('Notification error:',event);},onShow: ()=>{console.log('Notification shown');},});}
Permission Request Flow
asyncfunctionsetupNotifications(): Promise<boolean>{if(!BrowserNotification.isSupported()){showFallbackUI();returnfalse;}// Check current permissionif(BrowserNotification.isDenied()){showPermissionDeniedMessage();returnfalse;}if(BrowserNotification.isGranted()){returntrue;}// Request permissionconstresult=awaitBrowserNotification.requestPermission();if(Result.isErr(result)){showPermissionDeniedMessage();returnfalse;}returnresult.value==='granted';}
Grouped Notifications
asyncfunctionshowChatNotification(chatId: string,message: string): Promise<void>{// Using tag to replace existing notification for same chatawaitBrowserNotification.show('New Message',{body: message,tag: `chat-${chatId}`,// Will replace previous notification with same tagicon: '/icons/chat.png',});}
Service Worker Notifications
// For notifications when page is not focusedasyncfunctionshowBackgroundNotification(title: string,body: string): Promise<void>{constresult=awaitBrowserNotification.showViaServiceWorker(title,{
body,icon: '/icons/app.png',badge: '/icons/badge.png',vibrate: [200,100,200],actions: [{action: 'view',title: 'View'},{action: 'dismiss',title: 'Dismiss'},],});if(Result.isErr(result)){// Fall back to regular notificationawaitBrowserNotification.show(title,{ body });}}