react-native-push-notification
react-native-push-notification copied to clipboard
onNotification isn't gets called on receiving local notification.
onNotification is not even gets called on the example app on receiving notification.
While triggering instant local notification or scheduled notification, onNotification isn't gets called on receiving. It gets called after user perform an action on the received push notification. At the readme, it is stated that onNotification gets invoked as soon as a notification is received and when user performs an action. I have tested it for Android. But it is not happening. I checked both on emulator as well as real device.
Environment info
react-native info output:
System:
OS: macOS 10.15.3
CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Memory: 797.71 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
Yarn: 1.22.0 - /usr/local/bin/yarn
npm: 6.12.1 - ~/.nvm/versions/node/v12.13.1/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.8.4 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
Android SDK:
API Levels: 23, 28, 29
Build Tools: 28.0.3, 29.0.2
System Images: android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5900203
Xcode: 11.3.1/11C505 - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_222 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.11.0 => 16.11.0
react-native: 0.62.2 => 0.62.2
npmGlobalPackages:
react-native: Not Found
Library version: 3.3.1
Hi @devanshujain95
I think the documentation is not clear on this, but this would not have sense to trigger onNotification when you are already in the running application.
Hi @devanshujain95 I think the documentation is not clear on this, but this would not have sense to trigger
onNotificationwhen you are already in the running application.
Though it makes sense that there is no need to trigger onNotification in an already running application, but I want to know about notification arrival when the app is in background or closed.
And for the documentation: Please refer the screenshot.
It is clearly stated as comment above onNotification. "(Called when a remote or local notification is opened or received)"

I think your are looking for something else: https://rnfirebase.io/messaging/usage#background--quit-state-messages But this can be achieved with remote notification only. In a local way, i would suggest https://github.com/transistorsoft/react-native-background-fetch This documentation is not correctly stated, I will change this. Android implementation follow the same implementation as iOS https://github.com/react-native-community/push-notification-ios
@Dallas62 So, as far by this discussion, there is no way by which onNotification gets called when local notification is received?
Using background fetch is not good option considering the fetch occurs in every 15 mins or more. No way to make it a less duration gap.
Is there the other way I could get to know about the notification arrival?
Take a look at https://github.com/zo0r/react-native-push-notification/issues/1413 He has the same use case
It could be great if the library supports that onNotification gets called on receiving local notifications. There is no sense to use another library or Headless.JS that react now supports if I am using already a library that seems to give me all functionality except one!
@Dallas62 Can you suggest a way I can get to know when local notification is received for both Android/iOS?
Thanks
Hi @devanshujain95 For remote notification it's works, only local notification are not supported. I need to make some investigation, but if iOS doesn't support this feature, this will cause compatibility breaks between iOS and Android.
@Dallas62 Thanks for your quick reply.
For iOS I can manage, but if android supports, it could be great help for me.
@Dallas62 I checked for iOS at push-notification-ios that this library uses internally. It supports local notification listener.
And I myself checked at iOS simulator, and it triggers onNotification as soon as local notification receives. This is the object I received for iOS - local notification:
{"alert": "My Notification Message", "badge": 10, "data": {}, "finish": [Function finish], "foreground": true, "message": "My Notification Message", "sound": null, "userInteraction": false}
@devanshujain95 By local notification, you mean both:
- localNotification
- localNotificationSchedule
I will check this later, today is already full 😄
@Dallas62 Preferably localNotificationSchedule.
This is needed by us as well. A great example is when the app is in the background with say location services on and an onLocation event fires triggering a local notification which should do something on opening.
Any update on this? I want to make the schedule notification disappear after some time. I was able to do that with Remote notifications because onNotification was triggering on receiving a remote notification but I can't do that with local notification.
@Dallas62 I checked for iOS at push-notification-ios that this library uses internally. It supports local notification listener.
And I myself checked at iOS simulator, and it triggers onNotification as soon as local notification receives. This is the object I received for iOS - local notification:
{"alert": "My Notification Message", "badge": 10, "data": {}, "finish": [Function finish], "foreground": true, "message": "My Notification Message", "sound": null, "userInteraction": false}
Hi,
Can you share your code for iOS ?
I tested many settings and iOS and onNotification is not called when the app is not in background/killed or foreground if the user doesn't press the notification.
@Dallas62 onNotification for iOS is not called when the app is in background/killed state. It is only called when the app is running and a notification is received.
@devanshujain95 for me, on iOS, onNotification is triggered when the app is in a killed state but not triggered when the app is in the foreground or background.
This would be a great addition! I'm using scheduled local notifications and would love to be able to have my foreground-state app be aware of when a notification was delivered. Unless there is some other way to listen for this event?
This would be a great addition! I'm using scheduled local notifications and would love to be able to have my foreground-state app be aware of when a notification was delivered. Unless there is some other way to listen for this event?
I still haven't found a way and I still have a use case for this exact purpose. I think we need this functionality.
I met same issue with this, https://github.com/react-native-push-notification-ios/push-notification-ios/issues/63#issuecomment-625539451 is working for me. Adding this, then the app triggers onNotification even on receiving local notification in iOS
Is there any solution found yet for this? For Android too!!
Also experiencing this.