Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 696
Andrei Avatar asked Mar 09 '23 09:03

Andrei


2 Answers

Its common issue, the issue occurs when using splash screen. Edit the manifest like this:

<activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize" >
          <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.INFO" />
          </intent-filter>
        </activity>
like image 84
Ajmal Hasan Avatar answered Mar 24 '23 13:03

Ajmal Hasan


It's specifically written to not put the configuration in the React life-cycle. Otherwise Android won't have the right behavior (I had so mush pain with this point !). Here you have a nice tutorial: https://product.farewell.io/visible-react-native-push-notifications-that-work-on-both-ios-and-android-5e90badb4a0f It's bad explained, but the approach is perfect. Use a class to initialize and call the methods. Initialize the class in the top component as he suggests. It works good. Complete the missing informations with this tutorial: https://shift.infinite.red/react-native-node-js-and-push-notifications-e851b279a0cd Good luck !

like image 25
Pibo Avatar answered Mar 24 '23 13:03

Pibo