I have setup the Expo-Notifications, and I'm using local notifications as reminders, all works well until I need a notification to show while I'm inside the app. The notification always shows while the app is in background but I can't get it to work in the foreground.
App.tsx:
import * as Notifications from "expo-notifications";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
const App = () => {
const notificationListener = useRef();
const responseListener = useRef();
const [notification, setNotification] = useState(false);
useEffect(() => {
const getPerm = async() => {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus
if(existingStatus !== 'granted'){
const { lastStatus }:any = await Notifications.requestPermissionsAsync();
finalStatus = lastStatus
}
if (finalStatus !== "granted") {
console.log("Failed to get push token for push notification!");
return;
}
}
getPerm()
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
console.log("NOTIF: ", notification)
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log("RES: ",response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []) ....
How I schedule the notification in a different file:
const triggerNotifications = async () => {
await Notifications.scheduleNotificationAsync({
content: {
title: "You’ve got mail! ",
body: "Here is the notification body",
data: { data: "goes here" }
},
trigger: { seconds: 5 },
});
}