@@ -81,7 +81,15 @@ export interface WebNotificationOptions {
81
81
vibrate ?: number [ ]
82
82
}
83
83
84
- export interface UseWebNotificationOptions extends WebNotificationOptions , ConfigurableWindow {
84
+ export interface UseWebNotificationOptions extends ConfigurableWindow , WebNotificationOptions {
85
+ /**
86
+ * Request for permissions onMounted if it's not granted.
87
+ *
88
+ * Can be disabled and calling `ensurePermissions` to grant afterwords.
89
+ *
90
+ * @default true
91
+ */
92
+ requestPermissions ?: boolean
85
93
}
86
94
87
95
/**
@@ -93,22 +101,33 @@ export interface UseWebNotificationOptions extends WebNotificationOptions, Confi
93
101
* @param defaultOptions of type WebNotificationOptions
94
102
* @param methods of type WebNotificationMethods
95
103
*/
96
- export function useWebNotification ( defaultOptions : UseWebNotificationOptions = { } ) {
104
+ export function useWebNotification (
105
+ options : UseWebNotificationOptions = { } ,
106
+ ) {
97
107
const {
98
108
window = defaultWindow ,
99
- } = defaultOptions
109
+ requestPermissions : _requestForPermissions = true ,
110
+ } = options
111
+
112
+ const defaultWebNotificationOptions : WebNotificationOptions = options
100
113
101
114
const isSupported = useSupported ( ( ) => ! ! window && 'Notification' in window )
102
115
116
+ const permissionGranted = ref ( isSupported . value && 'permission' in Notification && Notification . permission === 'granted' )
117
+
103
118
const notification : Ref < Notification | null > = ref ( null )
104
119
105
- // Request permission to use web notifications:
106
- const requestPermission = async ( ) => {
120
+ const ensurePermissions = async ( ) => {
107
121
if ( ! isSupported . value )
108
122
return
109
123
110
- if ( 'permission' in Notification && Notification . permission !== 'denied' )
111
- await Notification . requestPermission ( )
124
+ if ( ! permissionGranted . value && Notification . permission !== 'denied' ) {
125
+ const result = await Notification . requestPermission ( )
126
+ if ( result === 'granted' )
127
+ permissionGranted . value = true
128
+ }
129
+
130
+ return permissionGranted . value
112
131
}
113
132
114
133
const { on : onClick , trigger : clickTrigger } : EventHook = createEventHook < Event > ( )
@@ -118,11 +137,13 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
118
137
119
138
// Show notification method:
120
139
const show = async ( overrides ?: WebNotificationOptions ) => {
121
- if ( ! isSupported . value )
140
+ // If either the browser does not support notifications or the user has
141
+ // not granted permission, do nothing:
142
+ if ( ! isSupported . value && ! permissionGranted . value )
122
143
return
123
144
124
- await requestPermission ( )
125
- const options = Object . assign ( { } , defaultOptions , overrides )
145
+ const options = Object . assign ( { } , defaultWebNotificationOptions , overrides )
146
+
126
147
notification . value = new Notification ( options . title || '' , options )
127
148
128
149
notification . value . onclick = clickTrigger
@@ -141,10 +162,8 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
141
162
}
142
163
143
164
// On mount, attempt to request permission:
144
- tryOnMounted ( async ( ) => {
145
- if ( isSupported . value )
146
- await requestPermission ( )
147
- } )
165
+ if ( _requestForPermissions )
166
+ tryOnMounted ( ensurePermissions )
148
167
149
168
// Attempt cleanup of the notification:
150
169
tryOnScopeDispose ( close )
@@ -167,6 +186,8 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
167
186
return {
168
187
isSupported,
169
188
notification,
189
+ ensurePermissions,
190
+ permissionGranted,
170
191
show,
171
192
close,
172
193
onClick,
0 commit comments