React-native-push-notification: Notification is not received on Android when app is in background

Created on 27 Jan 2020  路  6Comments  路  Source: zo0r/react-native-push-notification

After using BackHandler.exitApp() the app is moved into the background and onNotification callback is not triggered anymore. I've noticed that upon moving the app to the background manually, everything works fine.

Btw I have configured launch mode as suggested in other tickets:

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize"
        android:launchMode="singleTask">

I also tried to detect the moment when app goes to the foreground and use popInitialNotification but unfortunately the message received in this manner doesn't contain either data or message fields. Not sure why

    AppState.addEventListener('change', this._handleAppStateChange);
  _handleAppStateChange = (nextAppState) => {
    if (nextAppState === 'background') {
        PushNotification.popInitialNotification((ev) => {
          console.log('initial notification', ev)
         })
    }
  }

Most helpful comment

Adding this to AndroidManifest.xml

android:name=".MainActivity"
android:launchMode="singleTask"

This worked for me.

All 6 comments

I have same issue, does anybody can help?

same issue... anyone found solution?

Same issue, but some devices are getting and some devices are not getting push notifications, when we force kill the application. but actual behaviour should push notification even the app is closed. waiting for the solution, please POST :)

Adding this to AndroidManifest.xml

android:name=".MainActivity"
android:launchMode="singleTask"

This worked for me.

In my case, I have a splash screen activity that was apparently causing the problem.

In my AndroidManifest.xml, unchanged:

        <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>

original SplashActivity.java:

//...
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

new SplashActivity.java with the fix:

//...
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        Intent oldIntent = getIntent();
        Bundle extras = oldIntent.getExtras();
        if (extras != null) {
            intent.putExtras(extras);
        }

        startActivity(intent);
        finish();
    }
}

With the above fix, onNotification is being called when I tap on a notification while the app is in the background.

In my case, I have a splash screen activity that was apparently causing the problem.

In my AndroidManifest.xml, unchanged:

        <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>

original SplashActivity.java:

//...
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

new SplashActivity.java with the fix:

//...
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        Intent oldIntent = getIntent();
        Bundle extras = oldIntent.getExtras();
        if (extras != null) {
            intent.putExtras(extras);
        }

        startActivity(intent);
        finish();
    }
}

With the above fix, onNotification is being called when I tap on a notification while the app is in the background.

what about for ios. can you help me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anativ picture anativ  路  3Comments

nguyenvanphuc2203 picture nguyenvanphuc2203  路  3Comments

ssolida picture ssolida  路  3Comments

Kiran0791 picture Kiran0791  路  3Comments

uendar picture uendar  路  3Comments