React-native-push-notification: Can I run code when notification appears?

Created on 10 Oct 2016  Â·  16Comments  Â·  Source: zo0r/react-native-push-notification

In my case, and I have only tested with scheduled notifications, it seems that onNotification only fires when I open the app from the notification. I want to trigger some actions when the app is killed or on the background following a certain schedule.

Most helpful comment

Oops, sorry - pressed the wrong button.

All 16 comments

Is this a change in behaviour that you have observed as a result of the changes that were made?

I am pretty new to this project, so I guess not.

Sorry – I do not understand why this issue has been closed, can anyone briefly answer please?

Oops, sorry - pressed the wrong button.

I don't believe what you are after is supported.

Scheduled notifications result in a call to onNotification only if they happen while the app is running. Otherwise they appear as an alert in the notification centre.

I think if you want to run periodic scheduled tasks this isn't the right way to do it. You could send periodic remote push notifications from a server to your devices, that might work.

Otherwise you need to find a more suitable api - maybe the AlarmManager in Android? But I don't know of the equivalent in iOS.

Also, the change thats in master that is currently being tested will allow you rot run code when a notification arrives even if the app isn't running at all. It only works on Android though

I am looking only for android possibilities in this moment. Also will develop cloud tasks in the future, but for the moment only local. I am running a bit short of time, but no doubt the AlarmManager looks good.

@npomfret I'm using latest code from master. When I select a notification action, nothing happens and the notification stays, if the app is not running in background. If, however, the app runs in the background, selecting a notification action does in fact work and the notification gets removed.

So, I guess currently code doesn't run if app isn't running in the background.

The change that wen't into master means that code can run when a push notification is delivered and the app isn't running. It only works on Android and you need to set your push notifications up outside of the react lifecycle. Its not easy to explain, I think we need to add some docs to the readme.

Basically, you need to make sure the configure method is called as a side effect of importing your code, and not inside a react lifecycle method.

Sounds great, could we see a little example? I think I can try to figure it out, but still do not grasp the "as a side effect of importing your code".

Hi again,

I already configured such method as a "side effect" but any code I place under "onNotification" is solely ran if I open the app through clicking on the notification center (Android). Do I have to configure it any differently?

Update here.

Thanks for creating the TroubleShooting.md guide. I wanted to trigger JS code with a dead app but directly from a scheduled notification, which seems not to be possible.

Is there a way to achieve this? If so, I am open to PR.

I don't know if opening a terminated app from a scheduled notification is possible. @varungupta85 may have some ideas

@jsdario I just tested what happens if I click on a scheduled local notification when the app has been terminated and it just launched my app. It is possible that the onNotification may not be called in such scenarios. I didn't have time to test that piece. You should look into using popInitialNotification to check if you can access the local notification from there when the app is cold launched from a notification which is what it is supposed to do I guess.

I'll be closing since we have had no updates in a long time.

Hi! It seems like this is stlil an issue. Has there been any progress on this? #170 check my latest comment in that thread please.
And keep up the good work! @zo0r

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cookienawer picture cookienawer  Â·  3Comments

sungjinoh picture sungjinoh  Â·  3Comments

GastonEDiaz picture GastonEDiaz  Â·  3Comments

nidzovito picture nidzovito  Â·  3Comments

atteia picture atteia  Â·  4Comments