Schm1
Schm1

Reputation: 180

Android Firebase Push notification restarts app when clicked

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?

Upvotes: 8

Views: 4499

Answers (2)

Duc Trung Mai
Duc Trung Mai

Reputation: 2558

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

<activity
  ......
  android:launchMode="singleTask"
  >

Upvotes: 1

Shawn
Shawn

Reputation: 440

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.

Upvotes: 6

Related Questions