Skip to content

Commit eb0a357

Browse files
committed
Merge pull request #59 from owncloud/issue-1-browser-notification
Trigger browser web notification api when polling has a new notification
2 parents ba5837b + 15b0ce0 commit eb0a357

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

js/app.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
self._onNewNotification(n);
176176
}
177177
});
178-
// TODO check if any removed from JSON
178+
179179
for (var n in self.getNotifications()) {
180180
if (inJson.indexOf(self.getNotifications()[n].getId()) === -1) {
181181
// Not in JSON, remove from UI
@@ -229,7 +229,41 @@
229229
OCA.Notifications.notifications[notification.getId()] = notification;
230230
// Add to the UI
231231
OCA.Notifications.addToUI(notification);
232-
// TODO make a noise? Anything else?
232+
233+
// Trigger browsers web notification
234+
// https://github.com/owncloud/notifications/issues/1
235+
if ("Notification" in window) {
236+
if (Notification.permission === "granted") {
237+
// If it's okay let's create a notification
238+
OCA.Notifications.createWebNotification(notification);
239+
}
240+
241+
// Otherwise, we need to ask the user for permission
242+
else if (Notification.permission !== 'denied') {
243+
Notification.requestPermission(function (permission) {
244+
// If the user accepts, let's create a notification
245+
if (permission === "granted") {
246+
OCA.Notifications.createWebNotification(notification);
247+
}
248+
});
249+
}
250+
}
251+
},
252+
253+
/**
254+
* Create a browser notification
255+
*
256+
* @see https://developer.mozilla.org/en/docs/Web/API/notification
257+
* @param {OCA.Notifications.Notification} notification
258+
*/
259+
createWebNotification: function (notification) {
260+
var n = new Notification(notification.getSubject(), {
261+
title: notification.getSubject(),
262+
lang: OC.getLocale(),
263+
body: notification.getMessage(),
264+
tag: notification.getId()
265+
});
266+
setTimeout(n.close.bind(n), 5000);
233267
},
234268

235269
_shutDownNotifications: function() {

0 commit comments

Comments
 (0)