mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-10 10:56:34 +00:00
Fix push-notification toggle reset on failure (#701)
This commit is contained in:
parent
7303e55f63
commit
77cc77396b
@ -8,8 +8,7 @@
|
|||||||
<paper-toggle-button
|
<paper-toggle-button
|
||||||
hidden$='[[!pushSupported]]'
|
hidden$='[[!pushSupported]]'
|
||||||
disabled='[[loading]]'
|
disabled='[[loading]]'
|
||||||
on-change='handlePushChange'
|
checked='{{pushChecked}}'
|
||||||
checked='[[pushActive]]'
|
|
||||||
></paper-toggle-button>
|
></paper-toggle-button>
|
||||||
</template>
|
</template>
|
||||||
</dom-module>
|
</dom-module>
|
||||||
@ -32,9 +31,10 @@ class HaPushNotificationsToggle extends window.hassMixins.EventsMixin(Polymer.El
|
|||||||
document.location.hostname === '127.0.0.1')
|
document.location.hostname === '127.0.0.1')
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
pushActive: {
|
pushChecked: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
value: 'Notification' in window && Notification.permission === 'granted'
|
value: 'Notification' in window && Notification.permission === 'granted',
|
||||||
|
observer: 'handlePushChange',
|
||||||
},
|
},
|
||||||
loading: {
|
loading: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
@ -47,53 +47,45 @@ class HaPushNotificationsToggle extends window.hassMixins.EventsMixin(Polymer.El
|
|||||||
super.connectedCallback();
|
super.connectedCallback();
|
||||||
if (!this.pushSupported) return;
|
if (!this.pushSupported) return;
|
||||||
|
|
||||||
var el = this;
|
|
||||||
|
|
||||||
navigator.serviceWorker.ready.then(
|
navigator.serviceWorker.ready.then(
|
||||||
function (reg) {
|
(reg) => {
|
||||||
reg.pushManager.getSubscription().then(function (subscription) {
|
reg.pushManager.getSubscription().then((subscription) => {
|
||||||
el.loading = false;
|
this.loading = false;
|
||||||
el.pushActive = !!subscription;
|
this.pushChecked = !!subscription;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function () {
|
() => {
|
||||||
// no service worker.
|
// no service worker.
|
||||||
el._setPushSupported(false);
|
this._setPushSupported(false);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
handlePushChange(ev) {
|
handlePushChange(pushChecked) {
|
||||||
if (ev.target.checked) {
|
if (pushChecked) {
|
||||||
this.subscribePushNotifications();
|
this.subscribePushNotifications();
|
||||||
} else {
|
} else {
|
||||||
this.unsubscribePushNotifications();
|
this.unsubscribePushNotifications();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
subscribePushNotifications() {
|
subscribePushNotifications() {
|
||||||
var el = this;
|
|
||||||
|
|
||||||
navigator.serviceWorker.ready
|
navigator.serviceWorker.ready
|
||||||
.then(function (reg) {
|
.then(reg => reg.pushManager.subscribe({ userVisibleOnly: true }))
|
||||||
return reg.pushManager.subscribe({ userVisibleOnly: true });
|
|
||||||
})
|
|
||||||
.then(
|
.then(
|
||||||
function (sub) {
|
(sub) => {
|
||||||
var browserName;
|
let browserName;
|
||||||
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
|
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
|
||||||
browserName = 'firefox';
|
browserName = 'firefox';
|
||||||
} else {
|
} else {
|
||||||
browserName = 'chrome';
|
browserName = 'chrome';
|
||||||
}
|
}
|
||||||
|
|
||||||
return el.hass.callApi('POST', 'notify.html5', {
|
return this.hass.callApi('POST', 'notify.html5', {
|
||||||
subscription: sub,
|
subscription: sub,
|
||||||
browser: browserName
|
browser: browserName
|
||||||
}).then(function () {
|
|
||||||
el.pushActive = true;
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function (err) {
|
(err) => {
|
||||||
var message;
|
let message;
|
||||||
if (err.message && err.message.indexOf('gcm_sender_id') !== -1) {
|
if (err.message && err.message.indexOf('gcm_sender_id') !== -1) {
|
||||||
message = 'Please setup the notify.html5 platform.';
|
message = 'Please setup the notify.html5 platform.';
|
||||||
} else {
|
} else {
|
||||||
@ -104,36 +96,29 @@ class HaPushNotificationsToggle extends window.hassMixins.EventsMixin(Polymer.El
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
/* eslint-enable no-console */
|
/* eslint-enable no-console */
|
||||||
|
|
||||||
el.fire('hass-notification', { message: message });
|
this.fire('hass-notification', { message: message });
|
||||||
el.pushActive = false;
|
this.pushChecked = false;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
unsubscribePushNotifications() {
|
unsubscribePushNotifications() {
|
||||||
var el = this;
|
|
||||||
|
|
||||||
navigator.serviceWorker.ready
|
navigator.serviceWorker.ready
|
||||||
.then(function (reg) {
|
.then(reg => reg.pushManager.getSubscription())
|
||||||
return reg.pushManager.getSubscription();
|
.then((sub) => {
|
||||||
})
|
|
||||||
.then(function (sub) {
|
|
||||||
if (!sub) return Promise.resolve();
|
if (!sub) return Promise.resolve();
|
||||||
|
|
||||||
return el.hass
|
return this.hass
|
||||||
.callApi('DELETE', 'notify.html5', { subscription: sub })
|
.callApi('DELETE', 'notify.html5', { subscription: sub })
|
||||||
.then(function () {
|
.then(() => {
|
||||||
sub.unsubscribe();
|
sub.unsubscribe();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(function () {
|
.catch((err) => {
|
||||||
el.pushActive = false;
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
console.error('Error in unsub push', err);
|
console.error('Error in unsub push', err);
|
||||||
/* eslint-enable no-console */
|
/* eslint-enable no-console */
|
||||||
|
|
||||||
el.fire('hass-notification', {
|
this.fire('hass-notification', {
|
||||||
message: 'Failed unsubscribing for push notifications.'
|
message: 'Failed unsubscribing for push notifications.'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user