Don't show notifications in Android while app is in foreground

The docs for Push Notifications explain that iOS automatically hides push notifications (but passes data) if app is in the foreground, while android shows the notifications no matter what.

Is there any way to not show android notifications while the app is in the foreground, but still get the data like in iOS?

If not, this feels like a major bug, as there are very few use cases where you would want system notifications popping up while the app is open. For apps like messaging, this makes expo notifications unusable.

Ok just in case someone else encounters this problem, in digging through the api docs, i found that you can use something like this

var appState
const handleAppStateChange = nextAppState => {
  appState = nextAppState
}

var notificationsListener
const handleNotification = notification => {
  // for now dismiss all notification if the app is foregrounded
  if(appState === 'active' && notification.origin === 'received'){
    Notifications.dismissNotificationAsync(notification.notificationId)
  }
}

So for this to work, I had to use React Native’s AppState event to track the state of the app (active or background), because the notification system is broken (always provides received even if in the background). Once you know the state, you can dismiss any incoming notification by the id.

4 Likes

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