Fix push notifications (#1585)

This commit is contained in:
Paulus Schoutsen 2018-08-20 11:49:13 +02:00 committed by GitHub
parent 8da969455b
commit 4e135681bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,19 +57,21 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
// We don't set loading to `false` so we remain disabled // We don't set loading to `false` so we remain disabled
} }
} }
handlePushChange(pushChecked) { handlePushChange(pushChecked) {
if (!this.pushSupported) return;
if (pushChecked) { if (pushChecked) {
this.subscribePushNotifications(); this.subscribePushNotifications();
} else { } else {
this.unsubscribePushNotifications(); this.unsubscribePushNotifications();
} }
} }
subscribePushNotifications() {
navigator.serviceWorker.ready async subscribePushNotifications() {
.then(reg => reg.pushManager.subscribe({ userVisibleOnly: true })) const reg = await navigator.serviceWorker.ready;
.then(
(sub) => { try {
const sub = await reg.pushManager.subscribe({ userVisibleOnly: true });
let browserName; let browserName;
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
browserName = 'firefox'; browserName = 'firefox';
@ -77,49 +79,40 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
browserName = 'chrome'; browserName = 'chrome';
} }
return this.hass.callApi('POST', 'notify.html5', { await this.hass.callApi('POST', 'notify.html5', {
subscription: sub, subscription: sub,
browser: browserName browser: browserName
}); });
}, } catch (err) {
(err) => { const message = err.message || 'Notification registration failed.';
let message;
if (err.message && err.message.indexOf('gcm_sender_id') !== -1) {
message = 'Please setup the notify.html5 platform.';
} else {
message = 'Notification registration failed.';
}
/* eslint-disable no-console */ // eslint-disable-next-line
console.error(err); console.error(err);
/* eslint-enable no-console */
this.fire('hass-notification', { message: message }); this.fire('hass-notification', { message });
this.pushChecked = false; this.pushChecked = false;
} }
);
} }
unsubscribePushNotifications() {
navigator.serviceWorker.ready
.then(reg => reg.pushManager.getSubscription())
.then((sub) => {
if (!sub) return Promise.resolve();
return this.hass async unsubscribePushNotifications() {
.callApi('DELETE', 'notify.html5', { subscription: sub }) const reg = await navigator.serviceWorker.ready;
.then(() => {
sub.unsubscribe(); try {
}); const sub = await reg.pushManager.getSubscription();
})
.catch((err) => { if (!sub) return;
/* eslint-disable no-console */
await this.hass.callApi('DELETE', 'notify.html5', { subscription: sub });
await sub.unsubscribe();
} catch (err) {
const message = err.message || 'Failed unsubscribing for push notifications.';
// eslint-disable-next-line
console.error('Error in unsub push', err); console.error('Error in unsub push', err);
/* eslint-enable no-console */
this.fire('hass-notification', { this.fire('hass-notification', { message });
message: 'Failed unsubscribing for push notifications.' this.pushChecked = true;
}); }
});
} }
_compDisabled(disabled, loading) { _compDisabled(disabled, loading) {