startForeground() does not show my Notification

Dmitry Frank picture Dmitry Frank · Jan 4, 2012 · Viewed 22.6k times · Source

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.

Answer

Luka Kama picture Luka Kama · Sep 1, 2012

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.