Expo Push Notification Foreground not working

Please provide the following:

  1. SDK Version: SDK 42
  2. Platforms(Android/iOS/web/all): iOS 14.7.1

Foreground notifications: Not working at all

Background Notifications: Working

Killed notificiations: Working

Token generation: Working

Permissions: Verified and working

What should I do to troubleshoot this properly? I have tried other methods of handling, and I believe I tried adding a notification property to app.json but nothing worked to my knowledge.

Thanks for your time!

// imports redacted, but contain expo notification, device etc


Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});
export default function App() {
  const [expoPushToken, setExpoPushToken] = useState<string|undefined>('');
  const [notification, setNotification] = useState<any>(false);
  const notificationListener = useRef<any>();
  const responseListener = useRef<any>();


  useEffect(() => {
    if(Device.isDevice){
      registerForPushNotificationsAsync().then(token => setExpoPushToken(token));

      // This listener is fired whenever a notification is received while the app is foregrounded
      notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
        setNotification(notification);
      });

      // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
      responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
        console.log(response);
      });
      return () => {
        Notifications.removeNotificationSubscription(notificationListener.current);
        Notifications.removeNotificationSubscription(responseListener.current);
      };
    } else {
      //
    }
  }, []);
  return(view stuff)
}
// outside of functional component
  async function registerForPushNotificationsAsync() {
    let token;
    if (Constants.isDevice) {
      const { status: existingStatus } = await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
        alert('Failed to get push token for push notification!');
        return;
      }
      token = (await Notifications.getExpoPushTokenAsync({ experienceId: '@Expo-project-name' })).data; // commented project name for security
    } else {
      alert('Must use physical device for Push Notifications');
    }
  
    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }
    return token;
  }

I just tested our app with

  • IOS 14.7.1, Iphone 11
  • Expo SDK 42
  • expo-notifications 0.3.12
  • build and submit with EAS, with newest version of eas cli
  • tested on TestFlight app

And it worked. I can receive notification while app is on foreground

I cannot say how your code is different from ours, your setNotificationHandler should do the trick. But maybe try to create a minimal example with their example Notifications - Expo Documentation, and see if you can make that work?

I found my solution. My experience ID was improperly capitalized and that caused the wrong Token to be generating, making foreground notifications not work.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.