Expo Push Notification Foreground not working
Asked Answered
K

1

1

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;
  }
Kokand answered 20/9, 2021 at 16:2 Comment(0)
K
2

the fix to this solution is in the experienceId

Make sure your experienceID matches EXACTLY what your expo project name is.

I had mine where the @username/project-name 'project-name' portion was lowercase, but my project was actually named in CAPITAL letters, so @username/PROJECT-NAME

That's the fix!

Kokand answered 20/9, 2021 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.