Capturing clicks on Android
See original GitHub issueForeground/background notifications are inconsistent at the moment (a common theme for all multi-platform push notification libraries).
On iOS, receiving a remote notification in foreground immediately triggers the onNotification
event handler with foreground: true
. If the app is in background when the notification arrives, clicking the notification will trigger the onNotification
handler with foreground: false
.
On Android, foreground notifications work the same. But in background, when the notification arrives, the onNotification
handler triggers immediately with foreground: false
. Clicking the notificiation opens the app of course, but doesn’t trigger the event handler again.
My goal is to execute some code if and only if the user clicks on a certain notification. Are there any plans to homogenize the behavior across platforms, or is there another way to determine when notifications are clicked on Android?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:10 (1 by maintainers)
@osungjin @Dean177 it seems there’s a clean, permanent solution on the way https://github.com/zo0r/react-native-push-notification/issues/183, I came up with a hack until then.
After
onNewIntent
went away in RN 0.30, my workaround is adding the INFO intent to our MainActivity in AndroidManifest.xml. Like so:This makes
getLaunchIntentForPackage
return MainActivity, but still launches the app with SplashActivity. It could very well have catastrophic side effects, but we haven’t experienced any yet.I forgot to say, in my original issue I wrote:
This was a bug in my setup, because our app has two activities (one for a first-time splash screen, and a different MainActivity) so
onNewIntent
in MainActivity.java was never called. Clicking the notification meant thatonCreate
in our SplashActivity.java was called instead. Thought I’d mention it it in case someone googles their way in here.