I am making a Radio like app in Android, in which I need to show the current playing channel in the notification bar with "Play/Pause" buttons. so what i need to do is that when user click on pause button song should be paused and play image should be displayed. I have done doing pause for sound from notification bar but now i need to show the play image instead of pause image, and i also need to put the seek bar to control the volume for music. Thanks in advance.
To update a notification, you simply call notificationManager.notify() again and pass it the same id that you gave it last time. if there's an existing notification with that id, it will update that one with the new information.
Typically for music playback or other persistent behaviors that the user should have control over, a foreground service is used. You use a regular Service and call startForeground(), passing it an id and a notification just like you would for NotificationManager. The benefit of this approach is that Android is less likely to terminate your service if it starts looking for processes to clean up as long as your service is running in the foreground. Updating the notification works the same way: call startForeground() again with the same id and a new notification object.
Related
I have a food ordering app and I need to inform the restaurants of a new order. I have a Capacitor app which often runs in the background of tablets/phones of the restaurant. As a result, they sometimes miss an order.
In order to solve this, it would be great if I could ring the device as if an alarm goes off or if the device gets a call. Then they can swipe away the notification to stop it or something like that, to make sure they saw it. I would choose the sound myself so that it isn't obnoxious.
Is anything like that possible?
yes it's possible but,
you should know the following:
for Android:
1-create a foreground service to keep notification appears to the
restaurants, in this case the restaurant can not hide the
notification.
once the restaurant receive the notification just
sent an event to the foreground service to handle the action and the
data.
start the order activity from foreground service, once the
activity is created just play a sound.
for IOS,
it should be the same but i don't know how to create a foreground service in IOS.
When I show a notification via service.startForeground(id, notification), the notification is set to be ongoing, which makes the app cumbersome to use. Is there a workaround to make a foreground notification non-ongoing so that it can be easily swiped away?
If not, I guess I would have to choose between using a background service or adding a "dismiss" action to the notification, which already contains some actions.
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:
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 :).
My app pops up a message count notification and sets it as "ongoing". There is a timer that re-sends the notification every 5 minutes, and the notification has the flag Notification.FLAG_ONGOING_EVENT , so that it can only get cleared by my app. And my app is set up to only use sound and vibrate if the count changes.
So, the idea is that if my app changes the count, it sends out an updated notification, which replaces the old one, and plays a sound and vibrates. But if the 5 minute timer tries to update the notification, but the count didn't change, the notification is still sent. but without sound or vibrate. This is done in case the notification somehow got cleared, I want it to pop back up, but if it's still there, I don't want the user to be re-notified.
I also save the message count so that it's remembered if the app is closed and then re-opened. The problem is that when that happens (if it's force-closed, for example), the old notification stays in the bar, but the newly opened app has no idea that's the case. So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
Thanks.
So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
No.
However, you can use deleteIntent to find out if the user cleared the Notification. Either that, or track down the cause of your "somehow got cleared" issue.