Dismissing MediaStyle notifications - android

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:

Related

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.

How do i remove a foreground notification in Android Lollipop?

I'm looking to stop/dismiss a foreground notification for a service for a mediaplayer, much similar to Google's implementation for Google Music.
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music it can.
This is completely different to how it is implemented on Android 4.4, where the notification starts only when you leave the app and removes itself when you go back into the app. I can't see how to implement this either considering the requirements for a service to have a notification.
Any help would be much appreciated.
How do i remove a foreground notification in Android Lollipop?
You can remove your own foreground Notification by calling stopForeground() from your Service, that called startForeground().
For instance in Google Music, if you are playing music then the the notification cannot be swiped away. However if you pause the music, you can swipe it away.
Presumably, they are updating the setOngoing() value for the Notification based upon whether or not the music is playing.
You can also remove the notification in a tricky way:
start 1st service with startForeground(1, new Notification());
start 2nd service with startForeground(1, new Notification()); and immediately stop it
As a result 1st service is still running in foreground but the notification is gone thanks to stopping 2nd service. I've found it by accident :).

How to make ongoing wearable notification without "Mute App" action

I am trying to make player notification with play/stop actions that is ongoing. I manage to make ongoing notification on wearable but i can't figure out how to make it without Mute app action that added automatically for ongoing notifications. TuneIn, Google Play Music and PlayerFm somehow manage to do this so there is way i just can't find it. May be anyone know how to do this?
I will answer my own question. Apparently there is no way to do this trough Notification. What all those apps do - they use RemoteControlClient to achieve this.

Alerting the user from a service (Android)

So, i have a service working in the background (when the application is closed) which connects to internet every 2 minutes and gets some data from a database, and if something's wrong i want to alert the user with some kind of a beep tone and a message on the screen or even better in the notification bar. Is it possible to do this and if yes, how?
I think the nicest solution would be to use a SystemBar Notification.
Using the Notification.Builder you can add a custom sound to your notification using setSound(Uri sound).
What you think of is called ´Notification´ in Android. You can find more information here on how to create some.

How to achieve this in Android?

I am looking at Avast, Lookout for example and I am trying to understand the concept of the implementation. So it is more like asking for direction for me.
Persistent App icon in Notification bar.
Am I correct to say there are function NotificationManager is able to do it?
Scan virus during app installation, I am not interested in virus scanning but the triggering mechanism.
Some kind of Android service bind to the main app?
Main app that can be bring up in the Notification menu.
A main app that remain trigger action to the bind services?
So what do I need to read to understand? NoticationManager, Services and ??
In short, I want to load a icon in the notification bar that can bring up my app. There is a background service that perform specific task for a set interval.
Yep, NotificationManager and Notification can help you with that.
You just need to create the notification with flag FLAG_ONGOING_EVENT (to make it persistent). Even better if your service IS REALLY performing some long-running task, if so, you can start your service via Service.startForeground which needs some 'ongoing' notification for running (notification is required to notify the user that there is some work going now).
For triggering app install event, you can use BroadcastReceiver with filter by Intent.ACTION_PACKAGE_ADDED.

Categories

Resources