startForeground() does not show my Notification - android

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.

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.

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

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.

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.

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

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

Related

What can be the reason behind the failure of setShowBadge(false)?

I am posting a notification for a background task and bring it to foreground with startForeground with a visible ongoing notification. When it completes, I replace it with a cancellable notification using the same notification id and the same channel I created with setShowBadge(false).
The scenario works correctly: It does not show badge for the ongoing task and it is successfully replaced with the non-ongoing version. Except, setShowBadge(false) does not work when I use it for the cancellable notification.
I checked the official sample and many examples and I also checked my code, I could not find any problems. So, there has to be an exceptional situation here. What can be the problem? What am I possibly missing here?
It depends on default launcher in use. Stock launcher will work as expected but some launchers have their own implementation of Notification badges and they've been using it since way before badges were supported officially. For those launchers, this flag won't work.
Even if the launchers use notification badges from channels, they might not care about this flag (like in your case). I would say try calling setNumber(0) and hope that it works.
mNotificationBuilder.setNumber(0)

Foregroundservice startforeground doesn't show notification

I have a channel defined, and notification defined that I send to startforeground with notification Id different from 0.
I also searched in google lot, but I can't find- why startforeground doesn't show the notification??????
Please help!
It's really frustrating me!!!!!
I just solved it.
I tried the app on other real device and it worked, so in my phone (when I tried the first time) I deleted the app and clear its data and then ran the app again and its worked.

Android Notifications disappear after some time

Android notifications disappear after some time, without touching it to cancel. What could cause this kind of problem? The notification is started in a Service.
Is that possible that the low memory causes this kind of problem?
Make sure that there is no any other service or project module, which does the following call NotificationManager.cancelAll() to cancel all the notifications. It is always better to keep the notifications controller over Unique ID and cancel only the notifications which are related to that part, by this the code won't touch the notifications posted by other project parts.
NotificationManager documentation.

Notification sent via PendingIntent does not show up

In one of Activity-derived class methods I'm trying to send a Notification, clicking on which will bring the Activity to foreground if my app is in background (not visible) right now. There are reasons why I don't use Service, but use Activity (that will hold PARTIAL_WAKE_LOCK) for that.
I remember Notification worked for me when I used it like this, but I sent it from Service. Now when I send it from Activity method and it doesn't show up, though I hear its notification sound.
So are there any reasons that prevent Notification show up sent from Activity method and how can it be solved?
Thank you.
I was not exactly precise copying Notification code sample. I removed setSmallIcon() call from code as I was too lazy to add icons. As a result system was really dropping my Intent, saying that no icon is provided for it and it'll be a crash in future releases.

Dismissing MediaStyle notifications

New Android MediaStyle notifications in Lollipop don't have a dismiss button. Looks like there is already a bug for it on Google Code.
Does anyone know what's a good workaround for this issue until the bug is resolved?
Should we just delay switching to MediaStyle? Or use one of the actions as the dismiss button?
Adding on to #ianhanniballake's answer:
For Pre-Lollipop use:
notificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowCancelButton(true).setCancelButtonIntent(createPlayIntent());
For post-Lollipop:
To start a media-player notification one must have surely used startForeground() to start the Media Service as a Foreground Service. The problem now is that this Service is not dismissible. EVEN IF we set the setOngoing(false).
The best practice followed is to make the Service dismissible in the paused state. To do that, when your Media Service receives a Pause state callback, call stopForeground(false). This stops the service, but keeps the notification alive. And it can now be dismissed.
Happy coding.
One mechanism which appears to work quite well is make the notification ongoing while music is playing and make it not ongoing (allowing it to be swipe dismissed) when the music is paused. This seems to be the technique that Google Music already uses.
Easy but probably not the most suitable solution is to just add an Action with a 'close' icon. Then simply provide a PendingIntent which will trigger when the icon is clicked:

Categories

Resources