urbanairship / airship-flutter

Flutter integration
Other
17 stars 17 forks source link

Android: App is started by push message from terminated state #198

Closed m-schmale closed 6 months ago

m-schmale commented 7 months ago

Preliminary Info

What Airship dependencies are you using?

airship_flutter: 7.3.1

Report

What unexpected behavior are you seeing?

We have the problem that at least on Android the app (or the main method) is started from the terminated state when we receive a push message. Can this be prevented? This is problematic because unnecessary API calls are made every time.

What is the expected behavior?

We expect the app not to be started from terminated state when we receive a push message.

What are the steps to reproduce the unexpected behavior?

  1. Add a print statement at the beginning of the main
  2. Terminate the app (not by force close or in background state)
  3. Send a push message

Could you please help us with this?

jyaganeh commented 6 months ago

Hi @m-schmale, I don't believe there's any way to prevent starting the app when a push notification is received on Android, since the system will send an intent to the app to handle the incoming notification.

One strategy I've seen is to check the value of WidgetsBinding.instance?.lifecycleState before making requests (or use a listener to observe state changes and act accordingly). When a push is received in the background, the state should be AppLifecycleState.detached vs. a state of AppLifecycleState.resumed that you'd see when the app is launched in the foreground.

m-schmale commented 6 months ago

@jyaganeh Thanks a lot, this was really helpful. 👍 I trigger the API calls in the initState and the didChangeAppLifecycleState now. I still have to test this more but it seems to be working just fine.