React-native-push-notification: Capturing clicks on Android

Created on 1 Jul 2016  路  10Comments  路  Source: zo0r/react-native-push-notification

Foreground/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?

Most helpful comment

@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:

<activity
  android:name=".MainActivity"
  android:label="@string/app_name">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.INFO" />
  </intent-filter>
</activity>
<activity
  android:name=".SplashActivity"
  android:theme="@style/SplashTheme">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

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.

All 10 comments

Try userInteraction in 2.0.1

This is nice and consistent on both platforms now, receiving in foreground gives foreground: true/userInteraction: false. Clicking a notification foreground: false/userInteraction: true. Thanks!

I forgot to say, in my original issue I wrote:

Clicking the notificiation opens the app of course, but doesn't trigger the event handler again.

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 that onCreate in our SplashActivity.java was called instead. Thought I'd mention it it in case someone googles their way in here.

@iggyfisk How can receive trigger if use rn 0.30+ ? It same case as two activities.

@iggyfisk Having a similar issue due to implementing a seperate splash screen. How did you work around this?

@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:

<activity
  android:name=".MainActivity"
  android:label="@string/app_name">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.INFO" />
  </intent-filter>
</activity>
<activity
  android:name=".SplashActivity"
  android:theme="@style/SplashTheme">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

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.

@iggyfisk thanks a bunch for taking the time to respond.

@iggyfisk how you manage to get user click? In Android, when user taps on notification I'm never able to get the "onNotification" callback.

I'm using react-native-navigation.

@iggyfisk i see ShortcutBadger uses getLaunchIntentForPackage, and it's gonna get the info. ain't this gonna cause problem when you're trying to set the badge? ain't no real launcher activity.
currently getting crash on xperia, and i think this is the cause...will report. But would love to here ppl thoughts/experience.

yap. confirmed. hack crashes when attempting badge setting on xperia. So i figured an alternative method to define the activity to launch notification method, via a new custom category (borrowing from browsables):

Added this to my main activity instead of main/info:

            <intent-filter android:label="notification_react_native">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.dieam.reactnativepushnotification.intent.category.NOTIFY" />
            </intent-filter>

Then supported it in my own fork:
https://github.com/kang-health/react-native-push-notification/blob/master/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java#L80

what do you think?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uendar picture uendar  路  3Comments

cookienawer picture cookienawer  路  3Comments

NiuQiaoling picture NiuQiaoling  路  3Comments

cidevant picture cidevant  路  3Comments

selimonline picture selimonline  路  3Comments