Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground service start once and display activity on click

I have 5 activities in my app. Every activity starts the same foreground service.

In onStartCommand method of the service foreground notification is created which unfortunately means that every call of startForegroundService() in any activity plays notification sound (even though the service is already running). How can I create the foreground notification only once or at least how not to play notification sound on successive startForegroundService() calls?

The other related question is: how can I go back to my application when I click the foreground notification? I have 5 activites and I would like to reopen the activity that was the last one the user was interacting with.

like image 651
Martin Dusek Avatar asked Dec 05 '25 20:12

Martin Dusek


1 Answers

#1. before starting the service just check if its already running or not. In that case this will help you https://stackoverflow.com/a/5921190/6413387

#2. To reopen your last opened activity, you need to update the pending intent of your notification. Hope you will find your answer here https://stackoverflow.com/a/20142620/6413387

like image 149
towhid Avatar answered Dec 08 '25 09:12

towhid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!