Skip to content

Commit ac2cf6f

Browse files
nickvergessenrullzer
authored andcommitted
Request the permissions for notifications via user interaction
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 26eb67b commit ac2cf6f

2 files changed

Lines changed: 65 additions & 32 deletions

File tree

src/App.vue

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<template>
22
<div v-if="!shutdown" class="notifications">
3-
<div ref="button" class="notifications-button menutoggle" :class="{ hasNotifications: notifications.length }"
4-
tabindex="0" role="button"
3+
<div ref="button"
4+
class="notifications-button menutoggle"
5+
:class="{ hasNotifications: notifications.length }"
6+
tabindex="0"
7+
role="button"
58
aria-label="t('notifications', 'Notifications')"
6-
aria-haspopup="true" aria-controls="notification-container" aria-expanded="false">
9+
aria-haspopup="true"
10+
aria-controls="notification-container"
11+
aria-expanded="false"
12+
@click="requestWebNotificationPermissions">
713
<img ref="icon" class="svg" alt=""
814
:title="t('notifications', 'Notifications')" :src="iconPath">
915
</div>
@@ -29,7 +35,12 @@
2935
</ul>
3036
<div v-else class="emptycontent">
3137
<div class="icon icon-notifications-dark" />
32-
<h2>{{ t('notifications', 'No notifications') }}</h2>
38+
<h2 v-if="webNotificationsGranted === null">
39+
{{ t('notifications', 'Requesting browser permissions to show notifications') }}
40+
</h2>
41+
<h2 v-else>
42+
{{ t('notifications', 'No notifications') }}
43+
</h2>
3344
</div>
3445
</transition>
3546
</div>
@@ -49,6 +60,7 @@ export default {
4960
5061
data: function() {
5162
return {
63+
webNotificationsGranted: null,
5264
hadNotifications: false,
5365
backgroundFetching: false,
5466
shutdown: false,
@@ -68,7 +80,7 @@ export default {
6880
iconPath: function() {
6981
var iconPath = 'notifications'
7082
71-
if (this.notifications.length) {
83+
if (this.webNotificationsGranted === null || this.notifications.length) {
7284
if (this.isRedThemed()) {
7385
iconPath += '-red'
7486
}
@@ -89,6 +101,8 @@ export default {
89101
// Bind the button click event
90102
OC.registerMenu($(this.$refs.button), $(this.$refs.container), undefined, true)
91103
104+
this.checkWebNotificationPermissions()
105+
92106
// Initial call to the notification endpoint
93107
this._fetch()
94108
@@ -209,6 +223,46 @@ export default {
209223
_shutDownNotifications: function() {
210224
window.clearInterval(this.interval)
211225
this.shutdown = true
226+
},
227+
228+
/**
229+
* Check if we can do web notifications
230+
*/
231+
checkWebNotificationPermissions: function() {
232+
if (!('Notification' in window)) {
233+
console.info('Browser does not support notifications')
234+
this.webNotificationsGranted = false
235+
236+
}
237+
238+
if (window.Notification.permission === 'granted') {
239+
console.debug('Notifications permissions granted')
240+
this.webNotificationsGranted = true
241+
return
242+
}
243+
244+
if (window.Notification.permission === 'denied') {
245+
console.debug('Notifications permissions denied')
246+
this.webNotificationsGranted = false
247+
return
248+
}
249+
250+
console.info('Notifications permissions not yet requested')
251+
},
252+
253+
/**
254+
* Check if we can do web notifications
255+
*/
256+
requestWebNotificationPermissions: async function() {
257+
if (this.webNotificationsGranted !== null) {
258+
return
259+
}
260+
261+
console.info('Requesting notifications permissions')
262+
window.Notification.requestPermission()
263+
.then((permissions) => {
264+
this.webNotificationsGranted = permissions === 'granted'
265+
})
212266
}
213267
}
214268
}

src/Components/Notification.vue

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ export default {
203203
})
204204
205205
// Parents: TransitionGroup > NotificationsList
206-
if (this.$parent.$parent.backgroundFetching) {
207-
this._triggerWebNotification()
206+
if (this.$parent.$parent.backgroundFetching
207+
&& this.$parent.$parent.webNotificationsGranted) {
208+
this._createWebNotification()
208209
}
209210
},
210211
@@ -228,31 +229,9 @@ export default {
228229
},
229230
230231
/**
231-
* Check if we do web notifications
232-
*/
233-
_triggerWebNotification: function() {
234-
// Trigger browsers web notification
235-
if ('Notification' in window) {
236-
if (Notification.permission === 'granted') {
237-
// If it's okay let's create a notification
238-
this._createWebNotification()
239-
240-
// Otherwise, we need to ask the user for permission
241-
} else if (Notification.permission !== 'denied') {
242-
Notification.requestPermission(function(permission) {
243-
// If the user accepts, let's create a notification
244-
if (permission === 'granted') {
245-
this._createWebNotification()
246-
}
247-
}.bind(this))
248-
}
249-
}
250-
},
251-
252-
/**
253-
* Create a browser notification
254-
* @see https://developer.mozilla.org/en/docs/Web/API/notification
255-
*/
232+
* Create a browser notification
233+
* @see https://developer.mozilla.org/en/docs/Web/API/notification
234+
*/
256235
_createWebNotification: function() {
257236
var n = new Notification(this.subject, {
258237
title: this.subject,

0 commit comments

Comments
 (0)