0
votes

I tried adding push notification with the expo, I am able to push notification from the server but even though the app is in foreground notification handler is not triggered, I have replicated the documentation example from expo-notifications

export default function App(props) {
  const [expoPushToken, setExpoPushToken] = useState('');
  const [notification, setNotification] = useState(false);
  const notificationListener = useRef();
  const responseListener = useRef();

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

    notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
     
      setNotification(notification);
     
    });

    
    responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
      console.log(respone)
    });

   
  }, []);
1

1 Answers

0
votes

registerForPushNotificationsAsync() should be an async function that would be in your App Component here. In the example from expo it is added bellow the component as convention.

Further more the 'response' would only be logged after you press the push notification.

The other listener notificationListener is setting a state in your example here, so is not being logged maybe.

If you are using a bare workflow also add useNextNotificationsApi: true to app.json in your android object.