7

When receiving a Firebase push notification, while the App is backgrounded, Firebase automatically displays a notification.

The pending Intent included in this notification seems to always include the FLAG_ACTIVITY_NEW_TASK flag, which will cause the App to be restarted when the notification is clicked, even if the App is already alive in the background.

Is there any way to prevent this behaviour and simply have onNewIntent(intent) on the main Activity invoked instead?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Schm1
  • 160
  • 1
  • 8

2 Answers2

5

The launchMode attribute of the activity affects how the activity is launched.

see:

https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

singleTop, singleTask, or singleInstance should be used to prevent the notification intent from creating a new activity instance.

The flag FLAG_ACTIVITY_NEW_TASK doesn't influence a new activity being created, but makes the launched activity the root of a new task.

see:

https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

and:

https://developer.android.com/guide/components/tasks-and-back-stack.html

Hope this helps.

Shawn
  • 430
  • 3
  • 7
  • Hi Shawn, you're absolutely right. I completely forgot to set the launchMode. Thanks :-) – Schm1 Nov 11 '16 at 19:44
  • I had a similar problem with react-native and react-native-splash-screen. If someone else is also experiencing this, try setting the launch mode of the splash screen activity to singleInstance. It worked for me. – Tiago Peres França Aug 22 '19 at 18:12
1

in AndroidManifest.xml set your Activity launchMode to singleTask should fix this:

<activity
  ......
  android:launchMode="singleTask"
  >
Duc Trung Mai
  • 2,141
  • 1
  • 24
  • 23