Files
etf-oglasi/frontend/hooks/usePushNotifications.tsx
ksan 14725e180a
CI/CD / Backend Unit Tests (push) Successful in 1m48s
CI/CD / Deploy (push) Successful in 1m38s
push notifications not working for some reason
2026-06-11 01:29:48 +02:00

47 lines
1.3 KiB
TypeScript

import { useEffect } from "react";
import messaging from "@react-native-firebase/messaging";
import notifee, { AndroidImportance, EventType } from "@notifee/react-native";
async function displayLocalNotification(title?: string, body?: string) {
if (!title && !body) return;
const channelId = await notifee.createChannel({
id: "default",
name: "General",
importance: AndroidImportance.HIGH,
});
await notifee.displayNotification({
title,
body,
android: {
channelId,
pressAction: { id: "default" },
},
});
}
export function usePushNotifications() {
useEffect(() => {
const unsubscribeMessage = messaging().onMessage(async remoteMessage => {
const title = remoteMessage.data?.title as string | undefined;
const body = remoteMessage.data?.body as string | undefined;
await displayLocalNotification(title, body);
});
const unsubscribeNotifee = notifee.onForegroundEvent(({ type, detail }) => {
if (type === EventType.PRESS) {
console.log("Notification tapped:", detail.notification);
}
});
return () => {
unsubscribeMessage();
unsubscribeNotifee();
};
}, []);
}