Notification Permission Denied in Angular
When users deny the browser notification permission, Notification.requestPermission() returns 'denied'. Your Angular app needs to handle this gracefully — you can't ask again, and attempting to create a notification throws an error.
Check Permission Before Requesting
@Injectable({ providedIn: 'root' })
export class NotificationService {
async requestPermission(): Promise<'granted' | 'denied' | 'default'> {
if (!('Notification' in window)) {
console.warn('Notifications not supported');
return 'denied';
}
if (Notification.permission === 'granted') {
return 'granted';
}
if (Notification.permission === 'denied') {
// Can't re-request — browser blocks the prompt
return 'denied';
}
// Only prompt if permission is 'default'
return await Notification.requestPermission();
}
async notify(title: string, options?: NotificationOptions): Promise<void> {
const permission = await this.requestPermission();
if (permission !== 'granted') {
this.showInAppNotification(title, options?.body);
return;
}
new Notification(title, options);
}
private showInAppNotification(title: string, body?: string): void {
// Fallback: show a toast or in-app notification
this.snackBar.open(`${title}: ${body}`, 'Close', { duration: 5000 });
}
}Best Practices
- Don't request on page load — ask after a user action
- Explain why before prompting:
@Component({ ... })
export class NotificationPromptComponent {
showPrompt = Notification.permission === 'default';
async enable() {
const result = await this.notificationService.requestPermission();
this.showPrompt = false;
}
}- Always have a fallback — in-app toasts, email notifications, or badge indicators
SSR Safety
import { isPlatformBrowser } from '@angular/common';
const supported = isPlatformBrowser(this.platformId) && 'Notification' in window;Bugsly tracks notification permission states across your user base, helping you understand what percentage of users have denied notifications and optimize your fallback experience.
Try Bugsly Free
AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.
Get Started FreeRelated Articles
How to Fix DatabaseError in Spring Boot In Production
Learn how to fix the DatabaseError in Spring Boot in production. Step-by-step guide with code examples.
Read moreHow to Fix Writablestream Error in Node.js
Fix Writablestream Error in your Node.js app. Understand the root cause and apply the right solution.
Read moreHow to Fix Writablestream Error in Next.js
A practical guide to resolving Writablestream Error in Next.js, with real code examples and debugging tips.
Read moreHow to Fix Rangeerror in Electron In Production
Learn how to diagnose and fix the rangeerror in Electron in production. Includes code examples and prevention tips.
Read more