Why do Android Push Notifications call Application create and trigger AppStartup

294 Views Asked by At

I have noticed that when I send a push notification (Firebase Messaging Service) to my device my Application object is created. This is without clicking on the notification. Simply the act of viewing the notification creates the application. Further, it also starts the Jetpack AppStartup library. I want to be able to use AppStartup and application create. But I don't want to launch that code when a push notification occurs.

Why does Android do this? Is this part of all android notification, or is it a feature of the third-party push notification sdk I am using? And is there a way in Application.create and AppStartup to distinguish a normal app launch from a push notification triggered launch?

Again, I'm not talking about the user clicking on the notification (and launching the app because of a deeplink). I'm talking about just looking at the notification in the notification dropdown.

2

There are 2 best solutions below

0
CommonsWare On BEST ANSWER

Why does Android do this?

Android is starting your app process to run code in your app. Creating an Application instance and calling onCreate() will be part of that, as will creating any ContentProvider objects. IIRC, Jetpack Startup uses a ContentProvider to get control early in your process, though I am not 100% certain of that.

The reason why Android is starting your app process is because your app is causing the Notification to be displayed — specifically, Firebase Cloud Messaging is doing that. If I remember the protocol correctly, Play Services is sending a broadcast Intent that Firebase Cloud Messaging in your app will respond to, and part of that code will be displaying the Notification.

And is there a way in Application.create and AppStartup to distinguish a normal app launch from a push notification triggered launch?

onCreate() of an Application subclass has no means of knowing what specifically caused the process to be created, as there can be many possible reasons. If by "AppStartup" you mean Jetpack Startup, I do not recall it having any options here, but I have not spent much time with its API.

0
user13014417 On

If you can, keep your initializers and Application class as minimal as possible and ensure they don't do or start any work. Defer any such work to the moment when your Activity component is created. In Activity.onCreate callback you can safely assume that your app is launched by user action, not by broadcast event, service start/binding or content provider request.

Google should actually fix this somehow, because bad actors could abuse firebase push notifications as a very cheap DDoS mechanism.

Consider a flashlight app that sends a web request to some server in it's Application.onCreate installed on a million Android devices. Send a push notification to every user of this flashlight app. The server will go down, due to 1M requests per second.

UPD: Note though that Android can sometimes spawn your app process without calling Application.onCreate in some cases. One such case known to me is backup.