Allow push notifications to suggest its device name (#2446)

* Allow push notifications to suggest its device name

* Lint
This commit is contained in:
Tommy Jonsson 2019-01-12 05:35:10 +01:00 committed by Paulus Schoutsen
parent 5ae599b1b2
commit d34dada9d8

View File

@ -20,6 +20,7 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
<paper-toggle-button <paper-toggle-button
disabled="[[_compDisabled(disabled, loading)]]" disabled="[[_compDisabled(disabled, loading)]]"
checked="{{pushChecked}}" checked="{{pushChecked}}"
on-change="handlePushChange"
></paper-toggle-button> ></paper-toggle-button>
`; `;
} }
@ -35,7 +36,6 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
type: Boolean, type: Boolean,
value: value:
"Notification" in window && Notification.permission === "granted", "Notification" in window && Notification.permission === "granted",
observer: "handlePushChange",
}, },
loading: { loading: {
type: Boolean, type: Boolean,
@ -63,12 +63,12 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
} }
} }
handlePushChange(pushChecked) { handlePushChange(event) {
// Somehow this is triggered on Safari on page load causing // Somehow this is triggered on Safari on page load causing
// it to get into a loop and crash the page. // it to get into a loop and crash the page.
if (!pushSupported) return; if (!pushSupported) return;
if (pushChecked) { if (event.target.checked) {
this.subscribePushNotifications(); this.subscribePushNotifications();
} else { } else {
this.unsubscribePushNotifications(); this.unsubscribePushNotifications();
@ -77,10 +77,9 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
async subscribePushNotifications() { async subscribePushNotifications() {
const reg = await navigator.serviceWorker.ready; const reg = await navigator.serviceWorker.ready;
let sub;
try { 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";
@ -88,12 +87,24 @@ class HaPushNotificationsToggle extends EventsMixin(PolymerElement) {
browserName = "chrome"; browserName = "chrome";
} }
const name = prompt("What should this device be called ?");
if (name == null) {
this.pushChecked = false;
return;
}
sub = await reg.pushManager.subscribe({ userVisibleOnly: true });
await this.hass.callApi("POST", "notify.html5", { await this.hass.callApi("POST", "notify.html5", {
subscription: sub, subscription: sub,
browser: browserName, browser: browserName,
name,
}); });
} catch (err) { } catch (err) {
const message = err.message || "Notification registration failed."; const message = err.message || "Notification registration failed.";
if (sub) {
await sub.unsubscribe();
}
// eslint-disable-next-line // eslint-disable-next-line
console.error(err); console.error(err);