Notifications Android & Desktop

This commit is contained in:
RobinLinus 2018-09-21 23:23:07 +02:00
parent 390c72c933
commit 44bd3edd7b

View file

@ -1,8 +1,8 @@
const $ = query => document.getElementById(query); const $ = query => document.getElementById(query);
const $$ = query => document.body.querySelector(query); const $$ = query => document.body.querySelector(query);
const isURL = text => /^((https?:\/\/|www)[^\s]+)/g.test(text.toLowerCase()); const isURL = text => /^((https?:\/\/|www)[^\s]+)/g.test(text.toLowerCase());
const isDownloadSupported = (typeof document.createElement('a').download !== 'undefined'); window.isDownloadSupported = (typeof document.createElement('a').download !== 'undefined');
const isProductionEnvironment = !window.location.host.startsWith('localhost'); window.isProductionEnvironment = !window.location.host.startsWith('localhost');
class PeersUI { class PeersUI {
@ -236,10 +236,8 @@ class ReceiveDialog extends Dialog {
this.$el.querySelector('#fileSize').textContent = this._formatFileSize(file.size); this.$el.querySelector('#fileSize').textContent = this._formatFileSize(file.size);
this.show(); this.show();
if (!isDownloadSupported) return; if (window.isDownloadSupported) return;
// $a.target = "_blank"; // fallback $a.target = "_blank"; // fallback
$a.target = "_system"; // fallback
$a.href = 'external:' + $a.href;
} }
_formatFileSize(bytes) { _formatFileSize(bytes) {
@ -335,7 +333,7 @@ class Notifications {
constructor() { constructor() {
// Check if the browser supports notifications // Check if the browser supports notifications
if (!('Notification' in window)) return; if (!('Notification' in window)) return;
// Check whether notification permissions have already been granted // Check whether notification permissions have already been granted
if (Notification.permission !== 'granted') { if (Notification.permission !== 'granted') {
this.$button = $('notification'); this.$button = $('notification');
@ -361,35 +359,53 @@ class Notifications {
const config = { const config = {
body: body, body: body,
icon: '/images/logo_transparent_128x128.png', icon: '/images/logo_transparent_128x128.png',
// vibrate: [200, 100, 200, 100, 200, 100, 400],
// requireInteraction: true
} }
if (serviceWorker && serviceWorker.showNotification) { try {
// android doesn't support "new Notification" if service worker is installed
return serviceWorker.showNotification(message, config);
} else {
return new Notification(message, config); return new Notification(message, config);
} catch (e) {
// android doesn't support "new Notification" if service worker is installed
if (!serviceWorker || !serviceWorker.showNotification) return;
return serviceWorker.showNotification(message, config);
} }
} }
_messageNotification(message) { _messageNotification(message) {
if (isURL(message)) { if (isURL(message)) {
const notification = this._notify(message, 'Click to open link'); const notification = this._notify(message, 'Click to open link');
notification.onclick = e => window.open(message, '_blank', null, true); this._bind(notification, e => window.open(message, '_blank', null, true));
} else { } else {
const notification = this._notify(message, 'Click to copy text'); const notification = this._notify(message, 'Click to copy text');
notification.onclick = e => document.copy(message); this._bind(notification, e => this._copyText(message, notification));
} }
} }
_downloadNotification(message) { _downloadNotification(message) {
const notification = this._notify(message, 'Click to download'); const notification = this._notify(message, 'Click to download');
if (window.isDownloadSupported) return; if (!window.isDownloadSupported) return;
notification.onclick = e => { this._bind(notification, e => this._download(notification));
document.querySelector('x-dialog [download]').click();
};
} }
_download(notification) {
document.querySelector('x-dialog [download]').click();
notification.close();
}
_copyText(message, notification) {
document.copy(message);
notification.close();
this._notify('Copied text to clipboard');
}
_bind(notification, handler) {
if (notification.then) {
notification.then(e => serviceWorker.getNotifications().then(notifications => {
serviceWorker.addEventListener('notificationclick', handler);
}));
} else {
notification.onclick = handler;
}
}
} }
class Snapdrop { class Snapdrop {