-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathuseNotification.ts
More file actions
99 lines (83 loc) · 3.58 KB
/
useNotification.ts
File metadata and controls
99 lines (83 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import Browser from 'webextension-polyfill';
import { SECOND } from '../utils/time';
export enum NotificationType {
DailySummaryNotification = 'daily-summary-notification',
WebSiteNotification = 'website-notification',
}
export async function useNotification(
notificationType: NotificationType,
title: string,
message: string,
): Promise<string> {
// Check if we have notification permissions
const hasPermission = await checkNotificationPermission();
console.log('[useNotification] Permission check result:', hasPermission);
if (!hasPermission) {
console.warn('[useNotification] Notification permission not granted');
return '';
}
await Browser.notifications.clear(notificationType);
console.log('[useNotification] Cleared existing notifications');
await new Promise(res => setTimeout(res, 3 * SECOND));
const notificationOptions = {
type: 'basic',
title: title,
message: message,
iconUrl: Browser.runtime.getURL('128x128.png'),
isClickable: true, // Make it clickable so it's more visible
requireInteraction: true, // Keep it visible until user interacts
};
console.log('[useNotification] Creating notification with options:', notificationOptions);
try {
const notificationId = await Browser.notifications.create(notificationType, notificationOptions);
console.log('[useNotification] Notification created successfully with ID:', notificationId);
// Add event listeners to track notification events
Browser.notifications.onShown?.addListener((id) => {
if (id === notificationId) {
console.log('[useNotification] Notification shown:', id);
}
});
Browser.notifications.onClosed?.addListener((id, byUser) => {
if (id === notificationId) {
console.log('[useNotification] Notification closed:', id, 'by user:', byUser);
}
});
return notificationId;
} catch (error) {
console.error('[useNotification] Error creating notification:', error);
return '';
}
}
export async function checkNotificationPermission(): Promise<boolean> {
try {
// Check if we have the notifications permission
const permission = await Browser.permissions.contains({ permissions: ['notifications'] });
console.log('[checkNotificationPermission] Extension has notifications permission:', permission);
// Also check if the browser allows notifications at all
if (typeof Notification !== 'undefined') {
console.log('[checkNotificationPermission] Browser Notification API available');
console.log('[checkNotificationPermission] Browser notification permission:', Notification.permission);
// If browser permission is 'default', request it
if (Notification.permission === 'default') {
console.log('[checkNotificationPermission] Requesting browser notification permission...');
const browserPermission = await Notification.requestPermission();
console.log('[checkNotificationPermission] Browser permission result:', browserPermission);
}
} else {
console.warn('[checkNotificationPermission] Browser Notification API not available');
}
return permission;
} catch (error) {
console.error('[checkNotificationPermission] Error checking notification permission:', error);
return false;
}
}
export async function requestNotificationPermission(): Promise<boolean> {
try {
const granted = await Browser.permissions.request({ permissions: ['notifications'] });
return granted;
} catch (error) {
console.error('Error requesting notification permission:', error);
return false;
}
}