question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onNotification not called when the app is first opened from notification tap

See original GitHub issue

Bug

Environment info

react-native info output:

ystem:
    OS: macOS 10.15.7
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 35.33 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.20.1 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.10 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 29, 30
      Build Tools: 28.0.3, 29.0.2, 29.0.3
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.7199119
    Xcode: 12.4/12D4e - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_121 - /usr/bin/javac
    Python: 2.7.15 - /Library/Frameworks/Python.framework/Versions/2.7/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.14.0 => 16.14.0 
    react-native: 0.62.2 => 0.62.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 7.2.3

Steps To Reproduce

onNotification: function (notification) {
    // do something
}

The above code only runs when the notification is tapped and if the app was previously opened. However, if the app was not opened, it never runs.

I’m trying to handle the case when the user taps on the notification and the app wasn’t opened / was killed, in order to react to it accordingly.

I see from RN logs that an initialProps object is added when the app opens from the notification on iOS, could this be used to add extra info?

Thanks!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
suryateja77commented, May 18, 2021

Landed on the same issue, for remote notifications, the onNotification is not called on tapping the notification. However, it gets called when the notification arrives. Is there any other method that is triggered on clicking the notification from the tray?

OS: Android

The implementation is pretty much the same as mentioned above. I went through many other issues that were reported but could not find a solution for this. Any help is appreciated.

Thanks in advance.

1reaction
miallocommented, Feb 1, 2022

We were using react-native-bootsplash and it was working just fine. Then we removed it and onNotification was no longer called. We tried a few things and eventually we realized (with a little bit of help from this SO answer) that were missing the following in the .MainActivity:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.INFO" />
</intent-filter>
so in total that now looks like this:
<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
  android:windowSoftInputMode="adjustResize"
  android:exported="true"
  android:launchMode="singleTask">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.INFO" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="nora"/>
  </intent-filter>
</activity>
<activity
  android:name=".SplashScreenActivity"
  android:theme="@style/BootTheme"
  android:launchMode="singleTask">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Android react-native-push-notification onNotification not called ...
When I click the notification (app is running in the background), it opens the app but does not call onNotification.
Read more >
Tapping on notification to open app not w… - Apple Community
It seems like you're not able to tap on notification to open apps on your iPhone and you came to us from a...
Read more >
Start an Activity from a Notification - Android Developers
Tapping Back should take the user back through the app's normal work flow to the Home screen, and opening the Recents screen should...
Read more >
react-native-push-notification - npm
If not using a built in Android color ( @android:color/{name} ) for the notification_color meta-data item. In android/app/src/main/res/values/ ...
Read more >
Receive messages in an Android app - Firebase - Google
A user tap on a notification opens the app launcher by default. ... This is used when no icon is set for incoming...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found