mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-25 22:07:20 +00:00
Fix push notifications (#1585)
This commit is contained in:
parent
8da969455b
commit
4e135681bc
@ -57,69 +57,62 @@ 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
|
|
||||||
.then(reg => reg.pushManager.subscribe({ userVisibleOnly: true }))
|
|
||||||
.then(
|
|
||||||
(sub) => {
|
|
||||||
let browserName;
|
|
||||||
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
|
|
||||||
browserName = 'firefox';
|
|
||||||
} else {
|
|
||||||
browserName = 'chrome';
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.hass.callApi('POST', 'notify.html5', {
|
async subscribePushNotifications() {
|
||||||
subscription: sub,
|
const reg = await navigator.serviceWorker.ready;
|
||||||
browser: browserName
|
|
||||||
});
|
|
||||||
},
|
|
||||||
(err) => {
|
|
||||||
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 */
|
try {
|
||||||
console.error(err);
|
const sub = await reg.pushManager.subscribe({ userVisibleOnly: true });
|
||||||
/* eslint-enable no-console */
|
|
||||||
|
|
||||||
this.fire('hass-notification', { message: message });
|
let browserName;
|
||||||
this.pushChecked = false;
|
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
|
||||||
}
|
browserName = 'firefox';
|
||||||
);
|
} else {
|
||||||
}
|
browserName = 'chrome';
|
||||||
unsubscribePushNotifications() {
|
}
|
||||||
navigator.serviceWorker.ready
|
|
||||||
.then(reg => reg.pushManager.getSubscription())
|
|
||||||
.then((sub) => {
|
|
||||||
if (!sub) return Promise.resolve();
|
|
||||||
|
|
||||||
return this.hass
|
await this.hass.callApi('POST', 'notify.html5', {
|
||||||
.callApi('DELETE', 'notify.html5', { subscription: sub })
|
subscription: sub,
|
||||||
.then(() => {
|
browser: browserName
|
||||||
sub.unsubscribe();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
/* eslint-disable no-console */
|
|
||||||
console.error('Error in unsub push', err);
|
|
||||||
/* eslint-enable no-console */
|
|
||||||
|
|
||||||
this.fire('hass-notification', {
|
|
||||||
message: 'Failed unsubscribing for push notifications.'
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
|
const message = err.message || 'Notification registration failed.';
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
this.fire('hass-notification', { message });
|
||||||
|
this.pushChecked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async unsubscribePushNotifications() {
|
||||||
|
const reg = await navigator.serviceWorker.ready;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const sub = await reg.pushManager.getSubscription();
|
||||||
|
|
||||||
|
if (!sub) return;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
this.fire('hass-notification', { message });
|
||||||
|
this.pushChecked = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_compDisabled(disabled, loading) {
|
_compDisabled(disabled, loading) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user