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 >
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}
0 commit comments