1

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 },
    });
}
Nefic
  • 43
  • 4
  • Hi @Nefic, have you found a solution for this? I'm having the same issue. – Rohit Parte Jul 21 '23 at 08:40
  • 1
    Hi @RohitParte, I think my team discovered a temp fix that involved using a different "slug" in app.json for Android and iOS. I'm not on that project anymore so I can't verify it. – Nefic Jul 31 '23 at 13:42

0 Answers0