My question is regarding Notification and NotificationManager,
in my app I use a Notification to alert the user, but it plays the mp3 I gave it only once, and vibrates only ones, I wish it to play a sound and vibrate until the user clear the Notification by pressing clear. I couldn't find an example that simplified it, what's the best way to achieve that?
Thank All that reply,
Amitos80
there are flags you can set for achieving desired result,
Notification notification = new Notification(mIcon, mTickerText, mWhen);
notification.setLatestEventInfo(mContext, mContentTitle, mContentText, mContentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(ID, notification);
This will work definitely.
If you find it useful dont forget to mark it as an answer.
Related
My Code:
mNotifyBuilder.setSmallIcon(R.drawable.ic_status_bar)
.setContentTitle("")
.setOngoing(true)
.setAutoCancel(false)
.setTicker("")
.setColor(ContextCompat.getColor(mContext, R.color.folderlist_bg_music))
.setChannelId(CHANNEL_ID)
.setContent(remoteViews).build();
Working fine on other phones, but not working on Vivo V7.
On first swipe the notification is removed and it reappears. But on second swipe, it dismisses completely.
Option 1:
You need to do the following way:
builder.setOngoing(true);
Option 2:
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Option 3:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_service_launcher)
.setContentTitle("My title")
.setOngoing(true)
.setContentText("Small text with details");
start a dummy foreground Service ...this will persist the notification while it's running.
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.
Try adding this:
notification.flags |= Notification.FLAG_NO_CLEAR;
You can try the following:
builder.setOngoing(true); // Can't cancel your notification (except notificationManager.cancel(); )
Refer to this Ongoing notification
This is phone specific issue. Same thing works for other phones but not for Vivo phones.
OEMs shouldn't deviate from basic implementation that has been provided by stock Android.
I am working over a project and i am facing a strange problem in notification.
Basically i want to play alarm in notification and for that i am adding sound i.e.uri in Notification Builder.
Here is my code:
builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title)
.setContentText(msg)
.setColor(getResources().getColor(R.color.white))
.addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent)
.setOngoing(true)
.setSound(Uri.parse(path));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WAKE_UP_ALARM_ID, notification);
Notification appears in panel but when i touched the notification panel to scroll down, music stops automatically.
I am unable to understand the scenario.
Please help me to sort out the problem.
Thanks
The flags that are used in notification builder
1.Notification.DEFAULT_SOUND:used to play sound.
2.Notification.FLAG_INSISTENT:this flag is makes your sound to ring continuously until you have done few operations on notification ie,
either you drag the bar or you click the bar.
3.Notification.FLAG_AUTO_CANCEL:this flag is used to automatically cancel the notification after you have seen it
I cast an LED notification this straight forward way:
NotificationManager notifMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifMgr.cancelAll();
Notification notif = new Notification();
notif.ledARGB = 0xff0000ff;
notif.ledOnMS = 99999;
notif.ledOffMS = 0;
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notifMgr.notify(1234, notif);
It let the LED just permanent light blue.
If, in that state, I receive for example a google talk message, it's notification overwrites my LED state, so the LED now blinks white (gtalk default).
If now my program creates another LED notification, for some reason it does not overwrite the gtalk LED notification, so the LED stays blinking white.
How can I make my notification overwrite other LED notivications? Obviously there must be a way, since gtalk overwrites my LED state.
Thank you!
Whichever app raises the notification first keeps control of the led until it's cleared by that app or the user clearing it.
You description sounds incorrect of gtalk overriding your notification if yours is currently on. I know a little about this as I'm the author of "lightflow"
How do you allow the device to vibrate or make a sound when a notification is launched.
Ive heard of the FLAGS. But how would i use them for Sound and Vibration?
Edit In Android v4 support library, there is NotificationCompat.Builder and the method setSound which will work if using that class instead. However, the info below will still work.
Notification notif ... //create your notification
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
That adds sound and vibrate.
Always remember to check the docs. They have a LOT of answers, such as this one:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
That has the info I posted above as well as info on how to use a custom sound or vibrate and also the entire process of creating a notification.
EDIT: Don't forget to include the Vibrate permission in your manifest.
Notification notification // new notification object.
notification.defaults = Notification.DEFAULT_ALL;
Shows all the default notifications. (sound, vibration, led)
When an event happens my handler calls my eventAlert() function that pops up a new notification. It doesnt matter if it is set with default flags or with custom sound, when notifiing comes the sound repeats itself just like with FLAG_INSISTENT. Even with FLAG_ONLY_ALERT_ONCE. If i specify a custom sound with Uri.parse it behaves the same way. All the same with vibration. However if a make a custom vibration, calling it with the Vibrator's vibreate(long[], int) function it only vibrates once as it should. What am i doing wrong, what is the most common mistake that leads here? How can i make it to vibrate and alert with sound only once? My eventAlert() is called once, im pretty sure. thx for your help!
Thanks for the answer, this piece of code is what most of us search for when creating notifications for first time:
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
FLAG_AUTO_CANCEL clears the notification from the notification bar/list when clicking it.