question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Notification is not received on Android when app is in background

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

4reactions
khanhkomcommented, May 13, 2020

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

2reactions
arslansajidcommented, Feb 14, 2020

Adding this to AndroidManifest.xml

<activity android:name=“.MainActivity” android:launchMode=“singleTask”

This worked for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase onMessageReceived not called when app in ...
When app is in background, Notification messages are delivered to the system tray. If the app is in the foreground, messages are handled...
Read more >
How exactly do apps not running "in the background" receive ...
When you install a new app on your phone that uses push notifications, and give it permission to access the internet, it registers...
Read more >
Android - Not receiving Background notification · Issue #3652
Not receiving push notification in android when an app is in background doesn't really help us help you. We need to see your...
Read more >
Receive messages in an Android app - Firebase - Google
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the...
Read more >
Restrictions on starting activities from the background
In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found