Andrei Andrei - 11 months ago 58
Android Question

react-native push notification onNotification doesn't trigger

I am using zo0r react-native-push-notification library.

"react": "16.0.0-alpha.12",
"react-native": "^0.45.1",
"react-native-push-notification": "^3.0.0"


This code runs every time I open an app:

PushNotification.configure({
onNotification: function(notification) {
console.log('onNotification');
ToastAndroid.show('onNotification', 3000);
}
});


I send local push notification from background service:

PushNotification.localNotification({
message: 'Hello World',
smallIcon: 'ic_launcher'
});


The notification gets delivered. When I click it,
onNotification
method doesn't get called, then when I receive another notification, it actually gets called. It seems like it works only if app is in memory atm.

Am I doing something wrong?

I have opened a GitHub issue as well.

Answer Source

In my code I configured notifications outside App class - that was an issue. I just moved the configuration to App constructor and now it works just fine, onNotification gets called with no problem:

class App extends React.Component {
    constructor(props) {
        super(props);
        PushNotification.configure({ ... });
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download