How can I disable showing notifications in my Expo app when the user is in a specific screen?
I am currently designing a chat screen, and I want to disable push notifications when the user is inside it.
Currently, I have implemented the following methods in my PushNotifications module:
export function setNotificationHandler(handler) {
ExpoNotifications.setNotificationHandler(handler);
}
export function hidePushNotifications() {
setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: false,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
}
export function unhidePushNotifications() {
setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
}
And in my Chat screen, in a useEffect, I am doing:
useEffect(() => {
// Do not show push notifications when the user is inside this screen
hidePushNotifications();
return () => {
// When unmounted, unhide push notifications
unhidePushNotifications();
};
}, []);
For me, this has sense, as I am "hiding" push notifications when the user enters the chat screen, and "unhiding" them when he leaves.
But for some reasons, the notifications still appear inside the chat screen. Why?
How can I solve this issue?