Native Apps für iOS und Android (Capacitor) mit Push über Firebase

Apps (mobile/):
- Capacitor-8-Hülle, lädt die Web-App von APP_URL – Inhalte ohne App-Update
- Android- und iOS-Projekt (Swift Package Manager) mit Firebase Messaging,
  nativer Sprachausgabe, Teilen, App, Statusleiste, Startbildschirm
- `npm run sync` überträgt APP_ID, Name, Adresse und Farbe aus mobile/.env
  in beide Projekte und richtet ein: App-Links (Android intent-filter,
  iOS Associated Domains), Push (Entitlements, Background Mode,
  AppDelegate, Benachrichtigungssymbol/-farbe), Offline-Seite;
  idempotent, prüft Eingaben, warnt bei fehlenden Firebase-Dateien
- `npm run assets` erzeugt Symbole und Startbildschirme aus Logo und
  Markenfarbe der Redaktion, dazu das Android-Benachrichtigungssymbol
- Schritt-für-Schritt-Anleitung für den Mac (mobile/README.md)

Server:
- Versand an App-Geräte über FCM HTTP v1 (signiertes JWT, ohne
  firebase-admin), eingebunden in alle Benachrichtigungen; abgemeldete
  Geräte werden entfernt (nur bei UNREGISTERED, nicht bei anderen Fehlern)
- Firebase-Dienstkonto in den Einstellungen hochladen (verschlüsselt),
  App-ID, Team-ID, SHA-256, Store-Links; /.well-known/apple-app-site-
  association und assetlinks.json; Smart-App-Banner, Store-Hinweis
- Server erkennt die App am User-Agent (kein Aufblitzen von
  „Als App installieren“); Datenschutz-Vorlage um Firebase ergänzt

Web-App in der App: Push über Firebase statt Web-Push, Vorlesen nativ,
Teilen nativ, Zurück-Taste (schließt erst Dialoge), Antippen einer
Benachrichtigung und App-Links öffnen die Seite (nur eigene Adressen).

Getestet mit nachgebauter Capacitor-Brücke und nachgebautem Firebase-Dienst.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-10-03 07:03:26 +00:00
parent 02171e4cfe
commit 856559f830
152 changed files with 7537 additions and 32 deletions

View file

@ -1,7 +1,8 @@
import webpush from 'web-push';
import { and, asc, eq, isNull, lte } from 'drizzle-orm';
import { db } from './db';
import { events, posts, pushMessages, pushSubscriptions, settings } from './db/schema';
import { events, nativePushTokens, posts, pushMessages, pushSubscriptions, settings } from './db/schema';
import { sendNative } from './fcm';
import { getSettings } from './settings';
import { addDays, formatLong, TZ, today } from '$lib/dates';
import { PUSH_TOPICS, type PushTopic } from '$lib/push';
@ -52,11 +53,13 @@ export function removeSubscription(endpoint: string) {
db.delete(pushSubscriptions).where(eq(pushSubscriptions.endpoint, endpoint)).run();
}
/** Empfänger je Thema – Browser (Web-Push) und App-Geräte zusammen */
export function subscriberCounts() {
const rows = db.select({ topics: pushSubscriptions.topics }).from(pushSubscriptions).all();
const web = db.select({ topics: pushSubscriptions.topics }).from(pushSubscriptions).all();
const app = db.select({ topics: nativePushTokens.topics }).from(nativePushTokens).all();
const counts = Object.fromEntries(Object.keys(PUSH_TOPICS).map((k) => [k, 0])) as Record<PushTopic, number>;
for (const r of rows) for (const t of r.topics) if (t in counts) counts[t as PushTopic]++;
return { total: rows.length, byTopic: counts };
for (const r of [...web, ...app]) for (const t of r.topics) if (t in counts) counts[t as PushTopic]++;
return { total: web.length + app.length, web: web.length, app: app.length, byTopic: counts };
}
// ─── Versand ──────────────────────────────────────────────────────────────────
@ -79,9 +82,15 @@ export async function sendToTopic(
.all()
.filter((s) => (topic === 'all' ? s.topics.length > 0 : s.topics.includes(topic)));
const appDevices = db
.select({ topics: nativePushTokens.topics })
.from(nativePushTokens)
.all()
.filter((d) => (topic === 'all' ? d.topics.length > 0 : d.topics.includes(topic))).length;
// Automatische Nachrichten ohne Empfänger nicht als „gesendet“ vermerken –
// sonst wäre z. B. eine Schließzeit-Erinnerung verbraucht, bevor jemand abonniert hat
if (opts.automatic && subs.length === 0) return null;
if (opts.automatic && subs.length === 0 && appDevices === 0) return null;
let messageId: number;
try {
@ -124,6 +133,10 @@ export async function sendToTopic(
})
);
}
// Native Apps (iOS/Android) über Firebase
const native = await sendNative(topic, { title: payload.title, body: payload.body ?? '', url: payload.url ?? '/' });
ok += native.ok;
failed += native.failed;
db.update(pushMessages).set({ recipients: ok, failures: failed }).where(eq(pushMessages.id, messageId)).run();
return { recipients: ok, failures: failed };
}