Tester-Wunsch 04.10.: „Benachrichtigung bringt dich auf kuerzestem Weg dorthin, wo es wichtig ist“. - Nachrichten: notifications.list liefert die conversationId (ueber das sortierte Tier-Paar), App und Web oeffnen direkt das Gespraech. - Push-Mitteilungen: Antippen fuehrte bisher nur in die App. Jetzt useNotificationTapRouting (useLastNotificationResponse, auch Kaltstart), wechselt bei Bedarf aufs betroffene eigene Tier. Push-Daten tragen dafuer petId, actorPetId, storyId, conversationId. - Eine gemeinsame Zielregel fuer Liste und Push (mobile/src/lib/ notification-route.ts, getestet): Nachricht → Gespraech, Kommentar/Pfote/ Erwaehnung/Paw-Back → Beitrag, Story → Story, Follow/Vorschlag → Profil, Geburtstag/Adoption/Impfung → Gesundheit, Rang → Einladen. - Bei Nachrichten und Storys fuehrt auch der Name zum Ziel; Story-Viewer laedt Name/Bild nach, wenn er per Push geoeffnet wird. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.8 KiB
TypeScript
40 lines
1.8 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import * as Notifications from "expo-notifications";
|
|
import { useRouter } from "expo-router";
|
|
import { useActivePet } from "../context/ActivePetContext";
|
|
import { notificationRoute, targetFromPushData } from "../lib/notification-route";
|
|
|
|
/**
|
|
* Antippen einer Push-Mitteilung führt direkt zum Ziel (Gespräch, Beitrag,
|
|
* Story, Profil, Gesundheit) — vorher öffnete ein Tipp nur die App.
|
|
* useLastNotificationResponse deckt laufende App und Kaltstart ab.
|
|
*
|
|
* Gilt die Mitteilung für ein anderes eigenes Tier, wird vorher dorthin
|
|
* gewechselt (Nachrichten und Gesundheit hängen am aktiven Tier).
|
|
* Läuft im (app)-Layout, also erst nach Login und mit geladenen Tieren.
|
|
*/
|
|
export function useNotificationTapRouting(): void {
|
|
const response = Notifications.useLastNotificationResponse();
|
|
const router = useRouter();
|
|
const { activePetId, setActivePetId, pets, isPending } = useActivePet();
|
|
const handledId = useRef<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!response || isPending) return;
|
|
if (response.actionIdentifier !== Notifications.DEFAULT_ACTION_IDENTIFIER) return;
|
|
const id = response.notification.request.identifier;
|
|
if (handledId.current === id) return;
|
|
handledId.current = id;
|
|
|
|
const target = targetFromPushData(response.notification.request.content.data as Record<string, unknown> | undefined);
|
|
Notifications.clearLastNotificationResponse();
|
|
if (!target) return;
|
|
|
|
if (target.recipientPetId && target.recipientPetId !== activePetId && pets.some((p) => p.id === target.recipientPetId)) {
|
|
setActivePetId(target.recipientPetId);
|
|
}
|
|
const route = notificationRoute(target, Date.now());
|
|
if (route) router.push(route);
|
|
}, [response, isPending, activePetId, pets, setActivePetId, router]);
|
|
}
|