Test-Knopf fuer App-Erinnerungen in den Einstellungen ergaenzen

This commit is contained in:
torsten 2026-11-04 11:34:01 +01:00
parent 6e30fea4e6
commit 7737b6a46b
5 changed files with 146 additions and 17 deletions

View file

@ -91,6 +91,24 @@ class PushSubscription {
]);
}
/**
* Ein bestimmtes Gerät eines Spielers (für den Test-Knopf).
*
* @param int $userId
* @param string $endpoint
* @return array|false
*/
public static function findForUserAndEndpoint($userId, $endpoint) {
if (!self::isAvailable() || $endpoint === '') {
return false;
}
$stmt = Database::getConnection()->prepare(
"SELECT * FROM push_subscriptions WHERE user_id = ? AND endpoint_hash = ?"
);
$stmt->execute([(int)$userId, hash('sha256', $endpoint)]);
return $stmt->fetch(\PDO::FETCH_ASSOC);
}
/**
* Löscht alle Abos eines Spielers.
*

View file

@ -232,13 +232,7 @@ class Roundmail {
// App-Erinnerung ist ein eigener Kanal: Sie hängt an der zweiten
// Checkbox (gespeichertes Push-Abo), nicht an gets_reminders.
$push = PushSubscription::sendReminder($userIds, [
'title' => 'Hoschis Tippspiel',
'body' => 'Es fehlen noch Tipps/Joker für ' . $round['name']
. '. Die Runde sperrt sich am ' . $lockTime->format('d.m.Y H:i') . '.',
'url' => rtrim($appUrl, '/') . '/index.php?page=tipps',
'tag' => 'reminder-' . (int)$round['id'],
]);
$push = PushSubscription::sendReminder($userIds, self::pushPayload($round, $lockTime, $appUrl));
if (empty($recipients) && $push['sent'] === 0 && $push['failed'] === 0 && $push['removed'] === 0) {
return $round['name'] . ': Erinnerung nötig, aber kein Empfänger aktiviert.';
@ -277,6 +271,26 @@ class Roundmail {
return $result;
}
/**
* Baut die Push-Nutzlast für eine Runde. Wird sowohl vom echten Erinnerungslauf
* als auch vom Test-Knopf in den Einstellungen verwendet, damit die Vorschau
* exakt wie die spätere Erinnerung aussieht.
*
* @param array $round
* @param \DateTime $lockTime
* @param string $appUrl
* @return array
*/
public static function pushPayload($round, $lockTime, $appUrl) {
return [
'title' => 'Hoschis Tippspiel',
'body' => 'Es fehlen noch Tipps/Joker für ' . $round['name']
. '. Die Runde sperrt sich am ' . $lockTime->format('d.m.Y H:i') . '.',
'url' => rtrim($appUrl, '/') . '/index.php?page=tipps',
'tag' => 'reminder-' . (int)$round['id'],
];
}
/**
* Löscht automatische Erinnerungsmails (Automailer, Betreff REMINDER_TOPIC), die älter als
* REMINDER_RETENTION_MONTHS Monate sind. Rundmails von Admins bleiben unberührt.

View file

@ -57,6 +57,7 @@ require __DIR__ . '/partials/header.php';
<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>
<button type="button" id="push-test-button" class="push-test-button" hidden>Test-Erinnerung senden</button>
</div>
<div class="form-group">
@ -73,10 +74,12 @@ require __DIR__ . '/partials/header.php';
.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; }
.settings-form .push-test-button { margin: 0.5em 0 0 164px; }
@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; }
.settings-form .push-test-button { margin-left: 0; }
}
</style>
@ -85,7 +88,8 @@ require __DIR__ . '/partials/header.php';
var group = document.getElementById('push-reminder-group');
var box = document.getElementById('push_reminders');
var hint = document.getElementById('push-reminder-hint');
if (!group || !box || !hint) {
var testButton = document.getElementById('push-test-button');
if (!group || !box || !hint || !testButton) {
return;
}
@ -105,6 +109,10 @@ require __DIR__ . '/partials/header.php';
hint.className = 'settings-hint' + (isError ? ' is-error' : '');
}
function syncTestButton() {
testButton.hidden = !box.checked;
}
function keyToBytes(base64url) {
var padding = new Array((4 - (base64url.length % 4)) % 4 + 1).join('=');
var base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
@ -123,10 +131,14 @@ require __DIR__ . '/partials/header.php';
credentials: 'same-origin',
body: JSON.stringify(payload || {})
}).then(function (response) {
if (!response.ok) {
throw new Error('Server meldet HTTP ' + response.status + '.');
}
return response.json();
return response.json().catch(function () {
return {};
}).then(function (data) {
if (!response.ok) {
throw new Error((data && data.error) ? data.error : 'Server meldet HTTP ' + response.status + '.');
}
return data;
});
});
}
@ -199,9 +211,37 @@ require __DIR__ . '/partials/header.php';
setHint(error && error.message ? error.message : 'Aktion fehlgeschlagen.', true);
}).then(function () {
box.disabled = false;
syncTestButton();
});
});
// Test-Erinnerung an genau dieses Gerät. Der Server nimmt denselben Text wie
// der echte Erinnerungslauf, nur mit eigenem Tag.
testButton.addEventListener('click', function () {
if (testButton.disabled) {
return;
}
testButton.disabled = true;
setHint('Test-Erinnerung wird gesendet …');
navigator.serviceWorker.ready.then(function (registration) {
return registration.pushManager.getSubscription();
}).then(function (subscription) {
if (!subscription) {
throw new Error('Dieses Gerät hat noch kein Abo. Bitte den Haken aus- und wieder einschalten.');
}
return post('push_test', { endpoint: subscription.endpoint });
}).then(function () {
setHint('Test-Erinnerung gesendet. Sie sollte jetzt auf diesem Gerät erscheinen.');
}).catch(function (error) {
setHint(error && error.message ? error.message : 'Test fehlgeschlagen.', true);
}).then(function () {
testButton.disabled = false;
});
});
syncTestButton();
// 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