How to suppress Android push notification for foregrounded app

Use case: A user in a chat window should not receive a push notification for a message received in that same chat.

Problem: Using a notification listener, we can detect when a PN is received, and we can sniff out the current route of the user to identify whether this PN is useful or not. In the event the PN isn’t useful, what’s the best strategy to suppress the PN from rendering? This forum post describes an approach with dismissNotificationAsync. In my attempts, this isn’t good enough because the PN is only dismissed after the phone vibrates and the PN flashes on screen.

(On iOS this isn’t an issue since a message with origin ‘received’ won’t render.)

Conclusion: This strikes me as a common use-case so I’m curious the approach that others have taken. In the event there isn’t a clean solution, my fallback is going to be to stop the PN from ever being sent in the first place

Hi @nsdub!

One of our engineers has shared their preferred method here

TL;DR- https://docs.expo.io/versions/latest/guides/push-notifications/#3-handle-receiving-andor-selecting-the-notification :+1:

Hey Charlie – Thanks for the speedy reply!

So in my current implementation I’m using the same Notification Handler approach that quinlanj references. The root of my question is: What’s the best way to actually do the suppressing of the notification in the handler? Because the docs (and the forum post I referenced) suggest that it’s dismissNotificationAsync. But again the behavior that I’m seeing on my Pixel 3a is that the notification only dismisses after it’s rendered for a moment. Is this the expected behavior of dismissNotificationAsync or have I missed something?

Ah okay I see what you mean, any chance you could you share a Snack that reproduces that behavior?

My understanding is that PNs can only be received on an actual device. Is that accurate?

I don’t yet know whether this behavior that I’m experiencing also applies to Local Notifications.

Yeah, that’s accurate! You can still run snacks on your own device, through the Expo app :smile:

Ahh right, that makes sense. Sure, lemme see about the Snack…

Briefly following up here…

I’ve been unable to recreate the issue with dismissNotificationAsync in a Snack. What I’ve noticed is that in my implementation dismissNotificationAsync behaves differently depending on what React Navigation screen of a StackNavigator the receiving user is on. I’m totally perplexed as to why this is, but my hunch is that it’s not an issue with dismissNotificationAsync.

Thanks again for your time, Charlie. I’ll continue to diagnose this offline.

1 Like

For any time travelers who reference this post:

I was unable to achieve the desired behavior in my dev environment, but I pushed to prod to see what would happen. On prod my implementation of dismissNotificationAsync catches about 95% of PNs. In the remaining 5%, the PN flashes on screen for about a second before being dismissed. So if you’re experiencing similar behavior and feeling brave, this is one approach worth trying.

2 Likes

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