| 394 | |
| 395 | |
| 396 | class Notifications { |
| 397 | |
| 398 | constructor() { |
| 399 | // Check if the browser supports notifications |
| 400 | if (!('Notification' in window)) return; |
| 401 | |
| 402 | // Check whether notification permissions have already been granted |
| 403 | if (Notification.permission !== 'granted') { |
| 404 | this.$button = $('notification'); |
| 405 | this.$button.removeAttribute('hidden'); |
| 406 | this.$button.addEventListener('click', e => this._requestPermission()); |
| 407 | } |
| 408 | Events.on('text-received', e => this._messageNotification(e.detail.text)); |
| 409 | Events.on('file-received', e => this._downloadNotification(e.detail.name)); |
| 410 | } |
| 411 | |
| 412 | _requestPermission() { |
| 413 | Notification.requestPermission(permission => { |
| 414 | if (permission !== 'granted') { |
| 415 | Events.fire('notify-user', Notifications.PERMISSION_ERROR || 'Error'); |
| 416 | return; |
| 417 | } |
| 418 | this._notify('Even more snappy sharing!'); |
| 419 | this.$button.setAttribute('hidden', 1); |
| 420 | }); |
| 421 | } |
| 422 | |
| 423 | _notify(message, body) { |
| 424 | const config = { |
| 425 | body: body, |
| 426 | icon: '/images/logo_transparent_128x128.png', |
| 427 | } |
| 428 | let notification; |
| 429 | try { |
| 430 | notification = new Notification(message, config); |
| 431 | } catch (e) { |
| 432 | // Android doesn't support "new Notification" if service worker is installed |
| 433 | if (!serviceWorker || !serviceWorker.showNotification) return; |
| 434 | notification = serviceWorker.showNotification(message, config); |
| 435 | } |
| 436 | |
| 437 | // Notification is persistent on Android. We have to close it manually |
| 438 | const visibilitychangeHandler = () => { |
| 439 | if (document.visibilityState === 'visible') { |
| 440 | notification.close(); |
| 441 | Events.off('visibilitychange', visibilitychangeHandler); |
| 442 | } |
| 443 | }; |
| 444 | Events.on('visibilitychange', visibilitychangeHandler); |
| 445 | |
| 446 | return notification; |
| 447 | } |
| 448 | |
| 449 | _messageNotification(message) { |
| 450 | if (document.visibilityState !== 'visible') { |
| 451 | if (isURL(message)) { |
| 452 | const notification = this._notify(message, 'Click to open link'); |
| 453 | this._bind(notification, e => window.open(message, '_blank', null, true)); |
nothing calls this directly
no outgoing calls
no test coverage detected