Migrate to push notification store

This commit is contained in:
Paulus Schoutsen 2016-08-21 15:20:16 -07:00
parent 5962786494
commit 9b5ae6e8de
2 changed files with 18 additions and 52 deletions

@ -1 +1 @@
Subproject commit bf4adea64549d0adb95597f1846d90bc47648c0e
Subproject commit 3a2a1b288e1685cd6e5ce656656f1e5adb87fe76

View File

@ -202,16 +202,17 @@ Polymer({
supportPush: {
type: Boolean,
value: 'PushManager' in window &&
(document.location.protocol === 'https:' ||
document.location.hostname === 'localhost' ||
document.location.hostname === '127.0.0.1'),
value: false,
bindNuclear: function (hass) {
return hass.pushNotificationGetters.isSupported;
}
},
pushToggleChecked: {
type: Boolean,
value: 'Notification' in window &&
Notification.permission === 'granted',
bindNuclear: function (hass) {
return hass.pushNotificationGetters.isActive;
}
},
},
@ -294,53 +295,18 @@ Polymer({
},
handlePushChange: function (ev) {
var subscribe = ev.target.checked;
if (ev.target.checked) {
this.hass.pushNotificationActions.subscribePushNotifications()
.then(function (success) {
this.pushToggleChecked = success;
}.bind(this));
// MVP, will move later.
var promise = navigator.serviceWorker.getRegistration().then(function (reg) {
if (!reg) {
throw new Error('No service worker registered');
}
return reg.pushManager.subscribe({
userVisibleOnly: true,
});
});
if (!subscribe) {
promise.then(function (sub) {
sub.unsubscribe();
});
return;
} else {
this.hass.pushNotificationActions.unsubscribePushNotifications()
.then(function (success) {
this.pushToggleChecked = !success;
}.bind(this));
}
promise.then(function (sub) {
var browserName;
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
browserName = 'firefox';
} else {
browserName = 'chrome';
}
return this.hass.callApi('POST', 'notify.html5', {
subscription: sub,
browser: browserName,
});
}.bind(this)).catch(function (err) {
var 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 */
console.error(err);
/* eslint-enable no-console */
this.hass.notificationActions.createNotification(message);
this.pushToggleChecked = false;
}.bind(this));
},
handleLogOut: function () {