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
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?
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.
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.