Skip to content

Commit a1753d9

Browse files
feat(useWebNotification): add requestPermissions option, return permissionGranted and ensurePermissions (#3325)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 429edda commit a1753d9

File tree

1 file changed

+35
-14
lines changed
  • packages/core/useWebNotification

1 file changed

+35
-14
lines changed

packages/core/useWebNotification/index.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ export interface WebNotificationOptions {
8181
vibrate?: number[]
8282
}
8383

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
8593
}
8694

8795
/**
@@ -93,22 +101,33 @@ export interface UseWebNotificationOptions extends WebNotificationOptions, Confi
93101
* @param defaultOptions of type WebNotificationOptions
94102
* @param methods of type WebNotificationMethods
95103
*/
96-
export function useWebNotification(defaultOptions: UseWebNotificationOptions = {}) {
104+
export function useWebNotification(
105+
options: UseWebNotificationOptions = {},
106+
) {
97107
const {
98108
window = defaultWindow,
99-
} = defaultOptions
109+
requestPermissions: _requestForPermissions = true,
110+
} = options
111+
112+
const defaultWebNotificationOptions: WebNotificationOptions = options
100113

101114
const isSupported = useSupported(() => !!window && 'Notification' in window)
102115

116+
const permissionGranted = ref(isSupported.value && 'permission' in Notification && Notification.permission === 'granted')
117+
103118
const notification: Ref<Notification | null> = ref(null)
104119

105-
// Request permission to use web notifications:
106-
const requestPermission = async () => {
120+
const ensurePermissions = async () => {
107121
if (!isSupported.value)
108122
return
109123

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
112131
}
113132

114133
const { on: onClick, trigger: clickTrigger }: EventHook = createEventHook<Event>()
@@ -118,11 +137,13 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
118137

119138
// Show notification method:
120139
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)
122143
return
123144

124-
await requestPermission()
125-
const options = Object.assign({}, defaultOptions, overrides)
145+
const options = Object.assign({}, defaultWebNotificationOptions, overrides)
146+
126147
notification.value = new Notification(options.title || '', options)
127148

128149
notification.value.onclick = clickTrigger
@@ -141,10 +162,8 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
141162
}
142163

143164
// On mount, attempt to request permission:
144-
tryOnMounted(async () => {
145-
if (isSupported.value)
146-
await requestPermission()
147-
})
165+
if (_requestForPermissions)
166+
tryOnMounted(ensurePermissions)
148167

149168
// Attempt cleanup of the notification:
150169
tryOnScopeDispose(close)
@@ -167,6 +186,8 @@ export function useWebNotification(defaultOptions: UseWebNotificationOptions = {
167186
return {
168187
isSupported,
169188
notification,
189+
ensurePermissions,
190+
permissionGranted,
170191
show,
171192
close,
172193
onClick,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy