33 lines
1.4 KiB
PHP
33 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/common.php';
|
|
|
|
/**
|
|
* Geräte der Android-App für Push-Benachrichtigungen.
|
|
* GET -> Status: ob der Server Push senden kann und wie viele Geräte des Mitarbeiters angemeldet sind
|
|
* POST {action: "register", token, deviceName?, appVersion?} Gerät anmelden (oder Token erneuern)
|
|
* POST {action: "unregister", token} Gerät abmelden (beim Ausloggen)
|
|
*/
|
|
api_require_method('GET', 'POST');
|
|
$staff = api_require_staff();
|
|
$pdo = get_db();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
$count = $pdo->prepare('SELECT COUNT(*) FROM push_devices WHERE staff_id = ?');
|
|
$count->execute([(int) $staff['id']]);
|
|
api_json(['serverConfigured' => fcm_configured(), 'devices' => (int) $count->fetchColumn()]);
|
|
}
|
|
|
|
$body = api_request_body();
|
|
$action = (string) ($body['action'] ?? 'register');
|
|
$token = trim((string) ($body['token'] ?? ''));
|
|
if (!fcm_token_valid($token)) api_error(422, 'TOKEN_INVALID', 'Das Geräte-Token ist ungültig.');
|
|
|
|
if ($action === 'unregister') {
|
|
fcm_unregister_device($pdo, (int) $staff['id'], $token);
|
|
api_json(['registered' => false]);
|
|
}
|
|
if ($action !== 'register') api_error(422, 'ACTION_INVALID', 'Unbekannte Aktion.');
|
|
|
|
fcm_register_device($pdo, (int) $staff['id'], $token, (string) ($body['deviceName'] ?? ''), (string) ($body['appVersion'] ?? ''));
|
|
api_json(['registered' => true, 'serverConfigured' => fcm_configured()]); |