60

I am trying to make my Service running in foreground. I tried to use this example (please look for the section "Running a Service in the Foreground"), but startForeground() does not actually show my notification. And no exceptions is thrown. To make it shown, I need to use NotificationManager like here explained. With NotificationManager my notification works, but i'm not sure that my Service is foreground after this "silent" call to startForeground().

What can be wrong?

EDIT: I just tested this sample project that should demonstrate startForeground(), but it does not work! I use API v7.0, I tested it both on emulator and real device (SE Xperia Neo). Notification does not appear.

EDIT2: if i try to call setForeground() then i got a warning setForeground: ignoring old API call.

I also tried to use startForegroundCompat() as described here, but effect is absolutelly the same. I check if my service is foreground using ActivityManager.RunningServiceInfo as described here, and I see that my service is not foreground.

5
  • Since you elected to not provide source code, it will be difficult to help you directly. Here is a sample project that uses startForeground() successfully. Commented Jan 4, 2012 at 12:57
  • I did not provide source code because of my code is exactly the same as in example i provided: goo.gl/xXIvc . Thanks for the sample project. Commented Jan 5, 2012 at 4:51
  • I tested this sample project, it does not work! I use API v7.0, i tested it both on emulator and real device (SE Xperia Neo). Notification does not appear. Commented Jan 5, 2012 at 5:06
  • The sample project works perfectly fine -- I just re-tested it on a Nexus One, Galaxy Nexus, Nexus S, HTC Desire S, and the "API v7.0" emulator. To start the service and display the Notification, press the "Start the Player" button. Commented Jan 5, 2012 at 12:28
  • @CommonsWare, thanks for your time. And, of course, i pressed the "Start the Player" button =). Very strange, but it does not work for me on SE Xperia Neo and and API v7.0 emulator. Tomorrow i will test it on some kind of Acer, but i think the problem is in some different place, but i have absolutely no idea what could be wrong. Commented Jan 5, 2012 at 15:44

6 Answers 6

148

I just noticed that startForeground() doesn't show the notification icon if the id parameter is set to 0...

startForeground(0, notification); // Doesn't work...

startForeground(1, notification); // Works!!!

I hope that it could help someone stuck on this.

9
  • 12
    and yet the documentation is woefully vague about what this id value even does... I am totally frustrated with the (IMO) poor documentation -_- Commented Nov 6, 2012 at 5:57
  • 18
    And I had 123 which didn't work, changed to 1 and it works! Commented May 20, 2013 at 3:01
  • 4
    @HerrGrumps The docs have been updated and now have a warning: "The integer ID you give to startForeground() must not be 0." (developer.android.com/guide/components/services.html). Better later than never =P Commented Nov 16, 2014 at 21:49
  • 2
    Do not use too big integers like 1337 , 13377331 , 137 . Just use 0x3, 0x1 Commented Jul 26, 2017 at 1:06
  • 1
    @RavindranathAkila I also used 123, but changing to 1 still doesn't work :( Commented Jan 29, 2018 at 16:53
51

in addition to the best answer. you should also check that have you called setSmallIcon. On my android phone, I cannot get what I expected without calling setSmallIcon

6
  • 2
    Worked for me as well
    – Nikaoto
    Commented Dec 10, 2017 at 12:05
  • 3
    Thanks for this. This isn't mentioned as required in the services documentation, but it certainly should be! Commented Sep 11, 2018 at 13:24
  • 1
    <Rant>Android's official documentation covers 10% of the subject.</Rant>
    – TheKalpit
    Commented Jan 27, 2019 at 19:44
  • 2
    +1 This answer is so useful, I spent all day trying to figure why the stupid notification didn't behave properly...
    – user000001
    Commented Mar 3, 2019 at 13:55
  • 2
    This was my issue as well; without setSmallIcon, the notification just said '<app name> is running'. Thanks for saving me time and pain!
    – aremmell
    Commented Dec 13, 2019 at 22:21
2

Is your service a started service or a bound service? I had the same issue with a bound service, but starting the service before binding it allowed me to call startForeground(int, notification) with and have the notification show up.

1
  • for desired-to-be-bounded Service best option would be to show notification/start foreground in onCreate, it will be called before binding (onStartCommand will be called soon and wiil get initial Intent, that too late for notifying/foreground). thanks for clue!!
    – snachmsm
    Commented Sep 15, 2021 at 21:24
1

This might be an old thread, but I'd like to add what I just learned which is not mentioned yet:

It is possible that a Service is still alive after stopSelf() is called because there are Activity that have bound to the Service. As a matter of fact, the startForeground() is just not going to show the notification nor giving any exception in this circumstance.

1

in my case there was no notification after startForeground(...) invoke because I used only .setSubText(...) for setting the message of it (because it is rendered with smaller font on most of devices). But some of devices Xiaomi Redmi Note 4 won't show any notification if You not set message by using .setContentText(...).

Hope that will help someone

0

DMitry. I have just suffered your problem and found the cause.

If your app is changing state of a COMPONENT PackageManager.setComponentEnabledSetting()) Android removes the service from foregraound and its notification icon.

Bug reported at Nov, 2011

1
  • 1
    Thanks for the reply, but my app do not change state of a COMPONENT, so this might be some other reason. Commented Jul 10, 2012 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.