Files
jrshikoku/stateBox/useNotifications.tsx
harukin-expo-dev-env 777b5c8acb feat: add live activity notifications for train tracking and station locking
- Implemented ExpoLiveActivity module for Android to manage live notifications.
- Added foreground service for train tracking and station locking notifications.
- Updated app permissions to include POST_NOTIFICATIONS.
- Enhanced FixedStation and FixedTrain components to support live notifications.
- Introduced new notification builders for train and station activities.
- Updated useCurrentTrain and useNotifications hooks to manage live notification state.
- Added notification channel for live tracking in Android.
2026-03-22 16:15:48 +00:00

268 lines
7.8 KiB
TypeScript

import React, {
createContext,
useContext,
useState,
useEffect,
FC,
useRef,
} from "react";
import { Platform } from "react-native";
import * as Notifications from "expo-notifications";
import * as Device from "expo-device";
import Constants from "expo-constants";
import { logger } from "@/utils/logger";
import { rootNavigationRef } from "@/lib/rootNavigation";
import { SheetManager } from "react-native-actions-sheet";
type initialStateType = {
expoPushToken: string;
};
const initialState = {
expoPushToken: "",
};
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
function handleRegistrationError(errorMessage: string) {
// エラーを適切に処理(必要に応じてユーザーに通知)
//throw new Error(errorMessage);
}
async function registerForPushNotificationsAsync() {
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.DEFAULT,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
Notifications.setNotificationChannelAsync("運行情報", {
name: "運行情報",
importance: Notifications.AndroidImportance.DEFAULT,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
Notifications.setNotificationChannelAsync("遅延速報EX", {
name: "遅延速報EX",
importance: Notifications.AndroidImportance.LOW,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
Notifications.setNotificationChannelAsync("怪レい列車BOT", {
name: "怪レい列車BOT",
importance: Notifications.AndroidImportance.LOW,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
Notifications.setNotificationChannelAsync("live_tracking", {
name: "列車追従・駅ロック",
importance: Notifications.AndroidImportance.LOW,
vibrationPattern: [0, 0, 0, 0],
lightColor: "#00B8FF",
});
}
if (Device.isDevice) {
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
handleRegistrationError(
"Permission not granted to get push token for push notification!"
);
return;
}
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ??
Constants?.easConfig?.projectId;
if (!projectId) {
handleRegistrationError("Project ID not found");
}
try {
const pushTokenString = (
await Notifications.getExpoPushTokenAsync({
projectId,
})
).data;
logger.info('Push token obtained:', pushTokenString);
return pushTokenString;
} catch (e) {
handleRegistrationError(`${e}`);
}
} else {
handleRegistrationError("Must use physical device for push notifications");
}
}
const NotificationContext = createContext<initialStateType>(initialState);
type Props = {
children: React.ReactNode;
};
export const useNotification = () => {
return useContext(NotificationContext);
};
export const NotificationProvider: FC<Props> = ({ children }) => {
const [expoPushToken, setExpoPushToken] = useState("");
const [notification, setNotification] = useState<
Notifications.Notification | undefined
>(undefined);
const notificationListener = useRef<Notifications.EventSubscription | null>(null);
const responseListener = useRef<Notifications.EventSubscription | null>(null);
const handledResponseIdRef = useRef<string | null>(null);
const resolveNotificationRoute = (
response: Notifications.NotificationResponse
): "delay-ex" | "strange-train" | "information" | null => {
const request = response.notification?.request;
const content = request?.content;
const data = (content?.data ?? {}) as Record<string, unknown>;
const triggerRaw = request?.trigger as any;
const channelId =
triggerRaw?.channelId ??
triggerRaw?.remoteMessage?.notification?.channelId ??
"";
const categoryIdentifier = (((content as any)?.categoryIdentifier as string | undefined) ?? "")
.toString()
.trim();
const dataCategory = (
(data.category as string | undefined) ??
(data.type as string | undefined) ??
""
)
.toString()
.trim();
const keySet = new Set(
[channelId, categoryIdentifier, dataCategory]
.filter(Boolean)
.map((v) => v.toLowerCase())
);
if (keySet.has("遅延速報ex") || keySet.has("trainfoex") || keySet.has("tra_info_ex")) {
return "delay-ex";
}
if (keySet.has("怪レい列車bot") || keySet.has("strange_train") || keySet.has("strangetrain")) {
return "strange-train";
}
if (keySet.has("運行情報") || keySet.has("information") || keySet.has("informations")) {
return "information";
}
const normalized = [
content?.title,
content?.subtitle,
content?.body,
channelId,
JSON.stringify(data),
]
.filter(Boolean)
.join(" ")
.toLowerCase();
if (
normalized.includes("遅延") ||
normalized.includes("遅延速報") ||
normalized.includes("trainfoex") ||
normalized.includes("tra_info_ex")
) {
return "delay-ex";
}
if (
normalized.includes("怪レい列車") ||
normalized.includes("怪レい") ||
normalized.includes("strange_train") ||
normalized.includes("strange train")
) {
return "strange-train";
}
if (
normalized.includes("運行情報") ||
normalized.includes("information") ||
normalized.includes("informations")
) {
return "information";
}
return null;
};
const routeFromNotification = (
response: Notifications.NotificationResponse,
retryCount = 0
) => {
const requestId = response.notification?.request?.identifier ?? "";
if (handledResponseIdRef.current === requestId) return;
const action = resolveNotificationRoute(response);
if (!action) return;
if (!rootNavigationRef.isReady()) {
if (retryCount < 8) {
setTimeout(() => routeFromNotification(response, retryCount + 1), 300);
}
return;
}
handledResponseIdRef.current = requestId;
switch (action) {
case "delay-ex":
rootNavigationRef.navigate("topMenu", { screen: "menu" });
setTimeout(() => {
SheetManager.show("JRSTraInfo");
}, 450);
break;
case "strange-train":
rootNavigationRef.navigate("positions", { screen: "Apps" });
break;
case "information":
rootNavigationRef.navigate("information");
break;
default:
break;
}
};
useEffect(() => {
registerForPushNotificationsAsync()
.then((token) => setExpoPushToken(token ?? ""))
.catch((error) => setExpoPushToken(`${error}`));
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
routeFromNotification(response);
});
Notifications.getLastNotificationResponseAsync().then((response) => {
if (response) {
routeFromNotification(response);
}
});
return () => {
notificationListener.current &&
notificationListener.current.remove();
responseListener.current &&
responseListener.current.remove();
};
}, []);
return (
<NotificationContext.Provider value={{ expoPushToken }}>
{children}
</NotificationContext.Provider>
);
};