Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 920
Schm1 Avatar asked Nov 11 '16 14:11

Schm1


2 Answers

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.

like image 171
Shawn Avatar answered Nov 17 '22 20:11

Shawn


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

<activity
  ......
  android:launchMode="singleTask"
  >
like image 43
Duc Trung Mai Avatar answered Nov 17 '22 19:11

Duc Trung Mai