quickstart-android: Google FCM getIntent returns null when app is not running or when app is in background

  • Android device: Nexus 5 or emulator
  • Android OS version: Android N or Android M
  • Google Play Services version: 9.2.1
  • Firebase/Play Services SDK version: 9.2.1

Steps to reproduce:

  1. Using the Firebase console (or via the REST API), send a notification with custom data (for e.g. key “discount” value “10” )
  2. app is in background or not running

Observed Results:

though, the notification does appear in the system tray, the data is NOT available in the intent… either in onNewIntent or in onCreate, the extras is null.

Expected Results:

According to the Firebase Notifications docs, when App State is Background and a Notification with data is sent, the notification appears in the system try and the data will be available in the extras of the intent.

@Override public void onCreatet() { Bundle extras = getintent().getExtras(); —> extras is always null when app is not running or when app is in background }

@Override
public void onNewIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    ---> extras is always null when app is not running or when app is in background
}

Remarks: The data is available when App State is in the Foreground. But the notification console is pretty much unusable with this bug because ALL (background, app not running, foreground) users need to be able to get the data in a notification. Otherwise, how does a developer know what data a user saw or didn’t see (e.g. whether a user was shown the discount 10% or not).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 47 (4 by maintainers)

Most upvoted comments

For those that come here looking for a solution to this. Check if the extras exist on the SplashActivity and put them into the MainActivity intent if they so.

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = new Intent(this, MainActivity.class);  
        if (getIntent().getExtras() != null) {
            intent.putExtras(getIntent().getExtras());
        }
        startActivity(intent);
        finish();
    }
}

Same here. Any updates? My main activity is splash activity.

My steps:

  1. App is killed
  2. Notification is received and I tap on notification in notification tray
  3. App is opened and intent with all push notification extras are there as expected. After that splash activity is finished and home activity is opened.
  4. I minimize the app and wait for a new notification.
  5. I press on notification and the app just maximizes and only onResume from home activity is called with empty intent extras. The Splash activity wasn’t started.

Any solutions?

i got a solution for that. just put below code in oncreate method of launcher activity.

if (bundle != null) {
        String value = bundle.getString("key");
        if (value != null) {

            startActivity(new Intent(MainActivity.this, secActivity.class));
        }
}

when app is in background or killed,FCM will not call onmessagerecieved method,but it will send data to system tray to display notification.so datapayload(sent from fcm console) will not be handled by onmessagerecieved method.when user click on notification,it will launch default activity of app and datapayload will be passed by intent .so making change in oncreate method of launcher activity(as above)we can get datapayload even when app is in background or killed.(ex key is sent by fcm console).when app is in foreground datapayload and will be handled by onmessagerecieved method of fcm service.

Hello, To receive notification when app is in background, there are two tricks:

  • When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default => in my case app launcher was Splashactivity; i exract Extra Bundle in Oncreate Method

  • If you want to process background message arrival, it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own “console” in order to enable marketing people to use it. => i make use of “https://fcm.googleapis.com/fcm/send” with curl

Tested and it works !

The problem reported by @pmuraus was fixed by Firebase.

Update your app to support the latest version of Firebase and google Play “10.2.1”. Try to do the same steps told by @pmuraus

And now it works. It receives. Because now the app behaved as it was killed by the system, even in background.

If you downgrade the library to “10.0.1” the problema appears again.

On the release of the Google Play Services description they told the fix on the Message Received lifecycle.

Google Play services updated to 10.2.1

“This release includes updates to provide compatibility with Android O Developer Preview 1. The most significant updates are internal changes to the Google Cloud Messaging (GCM) and Firebase Cloud Messaging (FCM) libraries and a change in the guaranteed lifecycle of GCM and FCM callbacks (such as onMessageReceived() and onTokenRefresh()) to 10 seconds. After 10 seconds, Android O considers such callbacks eligible for termination. To avoid having your process terminated before your callback is completed, perform only quick operations (like updating a local database) inside the callback, and use JobScheduler to schedule longer background processes (like syncing a database with a remote source).”

Same issue. Tried sending notification with custom data from firebase console. The intent has to no extras.

I was facing same issue and I got this solution: You can get getIntent().getExtras() values in the very activity that you use to be launched at start. See in you manifest file which activity you have used to launch and in that activity in onCreate function you will get keys and values in getIntent().getExtras().

@NidhinVasudev Not sure but it looks like oxygen OS handles , this different way, it may be possible oxygen OS , kills (force stop ) app when we swipe it from recent tasks , to save battery or to free up ram. I tried above code on samsung devices and stock android devices , plus custom rom devices like MIUI.