Android Firebase Push notification intent extras null

Cazimir Roman
2 min readDec 14, 2020

Problem

When testing the Firebase notifications the other day I noticed that a specific logic was not executed in my app upon tapping the notification in the notification drawer. I needed to route the user to a specific fragment in my app. This is the payload I send from Firebase:

You can get this information in Android in onCreate or onResume:

val extras = intent.extras

if(extras != null) {
extras.getString("regards")?.let {
scrollViewPager(2)
}
}

The above code checks is there are extras attached to the intent that opened the activity (in our case: the notification tap). If there are extras, check if the regards extra is available and scroll the viewpage to position 2 if the value is found. Sounds easy enough, right?

If works when the app is fully closed and no instance is running but once you have the app running in the background, there are no extras returned if using the above code snippet.

The solution

The solution is to override the onNewIntent() method and check the existence of the extras here as well. Now tapping on the notification while the app is in background will correctly forward the intent to this method and execute the desired logic.

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

val extras = intent?.extras

if(extras != null) {
extras.getString("regards")?.let {
scrollViewPager(2)
}
}
}

Before you go…

If you found this article helpful, click the ❤️ or 👏 button below or share the article on Facebook so your friends can benefit from it too.

--

--

Cazimir Roman

A curious developer with a passion for learning and creating innovative solutions to complex problems.