App-Erinnerungen per Web-Push (VAPID) ergaenzen
This commit is contained in:
parent
af9bb67331
commit
6e30fea4e6
12 changed files with 1160 additions and 19 deletions
|
|
@ -50,6 +50,15 @@ require __DIR__ . '/partials/header.php';
|
|||
<label for="gets_reminders" class="inline">Ich möchte E-Mail-Erinnerungen erhalten</label>
|
||||
</div>
|
||||
|
||||
<?php // Nur in der installierten App sichtbar; das Skript unten blendet die Zeile ein. ?>
|
||||
<div class="form-group" id="push-reminder-group" hidden
|
||||
data-vapid-key="<?php echo htmlspecialchars((string)$pushPublicKey); ?>">
|
||||
<label for="push_reminders">App-Erinnerung:</label>
|
||||
<input type="checkbox" id="push_reminders" <?php if (!empty($pushEnabled)) echo 'checked'; ?>>
|
||||
<label for="push_reminders" class="inline">Ich möchte Smartphone-Erinnerungen erhalten (App)</label>
|
||||
<p class="settings-hint" id="push-reminder-hint"></p>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<button type="submit">Ok</button>
|
||||
</div>
|
||||
|
|
@ -62,6 +71,151 @@ require __DIR__ . '/partials/header.php';
|
|||
.settings-form input[readonly] { background: #f4f4f4; color: #666; border: 1px solid #ccc; }
|
||||
.settings-form input.wide { width: 25em; max-width: 100%; }
|
||||
.settings-form button { margin-left: 164px; }
|
||||
.settings-hint { margin: 0.3em 0 0 164px; font-size: 0.9em; color: #555; }
|
||||
.settings-hint.is-error { color: #b00020; }
|
||||
@media (max-width: 600px) {
|
||||
.settings-form .form-group > label:first-child { width: auto; display: block; }
|
||||
.settings-form button { margin-left: 0; }
|
||||
.settings-hint { margin-left: 0; }
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
var group = document.getElementById('push-reminder-group');
|
||||
var box = document.getElementById('push_reminders');
|
||||
var hint = document.getElementById('push-reminder-hint');
|
||||
if (!group || !box || !hint) {
|
||||
return;
|
||||
}
|
||||
|
||||
var vapidKey = group.getAttribute('data-vapid-key') || '';
|
||||
var isStandalone = window.matchMedia('(display-mode: standalone)').matches
|
||||
|| window.navigator.standalone === true;
|
||||
var supported = 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window;
|
||||
|
||||
// Die Checkbox ist ausdrücklich nur für die installierte App gedacht.
|
||||
if (!isStandalone || !supported || !vapidKey) {
|
||||
return;
|
||||
}
|
||||
group.hidden = false;
|
||||
|
||||
function setHint(text, isError) {
|
||||
hint.textContent = text || '';
|
||||
hint.className = 'settings-hint' + (isError ? ' is-error' : '');
|
||||
}
|
||||
|
||||
function keyToBytes(base64url) {
|
||||
var padding = new Array((4 - (base64url.length % 4)) % 4 + 1).join('=');
|
||||
var base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
|
||||
var raw = window.atob(base64);
|
||||
var bytes = new Uint8Array(raw.length);
|
||||
for (var i = 0; i < raw.length; i++) {
|
||||
bytes[i] = raw.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function post(page, payload) {
|
||||
return fetch('/index.php?page=' + page, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify(payload || {})
|
||||
}).then(function (response) {
|
||||
if (!response.ok) {
|
||||
throw new Error('Server meldet HTTP ' + response.status + '.');
|
||||
}
|
||||
return response.json();
|
||||
});
|
||||
}
|
||||
|
||||
function createSubscription(registration) {
|
||||
return registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: keyToBytes(vapidKey)
|
||||
});
|
||||
}
|
||||
|
||||
function subscribe() {
|
||||
return navigator.serviceWorker.ready.then(function (registration) {
|
||||
return registration.pushManager.getSubscription().then(function (existing) {
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
return createSubscription(registration).catch(function (error) {
|
||||
// Abo mit einem alten VAPID-Schlüssel: erneuern.
|
||||
if (error && error.name === 'InvalidStateError') {
|
||||
return registration.pushManager.getSubscription().then(function (old) {
|
||||
return old ? old.unsubscribe() : null;
|
||||
}).then(function () {
|
||||
return createSubscription(registration);
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
box.addEventListener('change', function () {
|
||||
if (box.disabled) {
|
||||
return;
|
||||
}
|
||||
box.disabled = true;
|
||||
setHint('');
|
||||
|
||||
var action;
|
||||
if (box.checked) {
|
||||
action = Notification.requestPermission().then(function (permission) {
|
||||
if (permission !== 'granted') {
|
||||
throw new Error('Benachrichtigungen sind blockiert. Bitte in den Systemeinstellungen erlauben.');
|
||||
}
|
||||
return subscribe();
|
||||
}).then(function (subscription) {
|
||||
if (!subscription) {
|
||||
throw new Error('Abo konnte nicht angelegt werden.');
|
||||
}
|
||||
return post('push_subscribe', subscription.toJSON());
|
||||
}).then(function () {
|
||||
setHint('Erinnerungen erscheinen jetzt auf diesem Gerät.');
|
||||
});
|
||||
} else {
|
||||
action = navigator.serviceWorker.ready.then(function (registration) {
|
||||
return registration.pushManager.getSubscription();
|
||||
}).then(function (subscription) {
|
||||
return subscription ? subscription.unsubscribe() : null;
|
||||
}).then(function () {
|
||||
return post('push_unsubscribe', {});
|
||||
}).then(function () {
|
||||
setHint('Smartphone-Erinnerungen sind ausgeschaltet.');
|
||||
});
|
||||
}
|
||||
|
||||
action.catch(function (error) {
|
||||
if (box.checked) {
|
||||
box.checked = false;
|
||||
}
|
||||
setHint(error && error.message ? error.message : 'Aktion fehlgeschlagen.', true);
|
||||
}).then(function () {
|
||||
box.disabled = false;
|
||||
});
|
||||
});
|
||||
|
||||
// Beim Öffnen das Gerät still nachtragen, wenn Erinnerungen gewünscht sind
|
||||
// und die Erlaubnis schon erteilt ist (z. B. zweites Gerät). Bewusst kein
|
||||
// automatisches Abmelden: Auf einem geteilten Gerät kann das Abo zu einem
|
||||
// anderen Konto gehören.
|
||||
if (box.checked && Notification.permission === 'granted') {
|
||||
subscribe().then(function (subscription) {
|
||||
if (subscription) {
|
||||
return post('push_subscribe', subscription.toJSON());
|
||||
}
|
||||
}).catch(function () {
|
||||
// still: die Einstellungsseite bleibt nutzbar
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php require __DIR__ . '/partials/footer.php'; ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue