47 lines
1.3 KiB
TypeScript
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();
|
|
};
|
|
}, []);
|
|
}
|