I currently have a notification that is set as a headsup notification by having it play a sound and have max priority. I build it like this:
builder.setPriority(Notification.PRIORITY_MAX).setCategory(Notification.CATEGORY_ALARM)
.setContentTitle(getString(R.string.alarm_title))
.setContentText(getString(R.string.alarm_text))
.setSmallIcon(R.drawable.ic_alarm_black_24dp)
.setSound(alarm_sound, attributes);
Notification notif = builder.build();
notif.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_INSISTENT;
The problem is that if a text comes in while this notification is present (the notification is important, as it is attached to a wake up alarm) the text message sound completely stops the rest of the sound being played. Additionally my notification disappears before the text message notification which appears on top, so I never get to actually see my notification (which has buttons like dismiss alarm attached).
Related
How can I remove all notifications in the notification tray that were sent by my server when the user clicks on one notifiaction?
I have a chatting-app and the user gets a notification for each message (if the app is not in foreground). When the user clicks on one of those notifications the app will be brought to foreground / will be started. After that has happened, I want all other notifications in the notification bar to disappear as well.
No, you dont have to save the ids of all notifications, you can simply call:
NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
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 have an application that has a reminder feature. When it's time to remind the user of something, my application creates a notification, possibly using FLAG_INSISTENT to ensure the alarm is heard. Once the user interacts with my app to acknowledge the alarm, the app cancels the notification.
The user can launch the app either by pulling down the notification bar and tapping on my notification -- in which case everything is fine -- or by navigating to the app some other way, such as by launching it from the home screen. If the user uses the notification bar method, the FLAG_INSISTENT audio stops when the user touches the notification bar. But here's the problem: if the user enters the app directly without touching the notification bar. the audio for the FLAG_INSISTENT alarm keeps playing indefinitely -- even after my app cancels the notification. The only way a user can stop it is to pull down the notification bar (or reboot the device!).
I've been getting tons of bug reports from angry users ever since the optional FLAG_INSISTENT feature went live. It doesn't seem specific to one platform; users reporting this bug have hardware including a Motorol Razr Maxx HD, Samsung Galaxy Note, and HTC EVO 4G LTE. I've had frustrated users report that they resorted to uninstalling the app to stop the noise, and even then said it wouldn't stop. Searching the web has been fruitless.
The notifications are being created in more-or-less the garden variety way:
notification = new Notification(
R.drawable.icon,
message,
System.currentTimeMillis()
);
if (userDefinedaCustomSound) {
notification.sound = Uri.parse(userSelectedReminderSound);
} else {
notification.defaults |= DEFAULT_SOUND;
}
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.DEFAULT_VIBRATE;
if (userWantsContinuousAlarm) {
notification.flags |= Notification.FLAG_INSISTENT;
}
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_BAR_ID, notification);
And are being cancelled thusly:
nm.cancel(NOTIFICATION_BAR_ID);
I've tried adding the FLAG_AUTO_CANCEL to the notification; that has no effect. As a workaround, I've also tried modifying my cancel method so that it first issues a new notification with no sound, and without FLAG_INSISTENT, then cancels; again, the audio just keeps on playing.
Any ideas?
I faced the same problem. The audio keep on playing after the notification was cancelled.
I formulated this work around,
Perform a normal notification cancelled.mNotificationManager.cancel(getNotificationID(event));
Immediately Create another notification with sound. Use the default Alarm.
Immediately cancel the notification.
Note:
The sound played as part of the notification was not the default.
method getNotificationID(event) always return the same constant for the same event object type.
Notification with sound played using the default will stop when the notification is cancelled.
I set the sound using builder using this
setSound(Uri.parse(this.sharedPreferences.getString(key,"")))
From the observations, I think it might be a bug. The reference to the ringtone object was not properly retained so when the cancel was called, it failed to call on the ringtone .stop() or was unable to do so.
Hope you can used it too.
NotificationManager mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(getNotificationID(event));
audioAlarmTriggered.remove(event.sensortype);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setAutoCancel(true)
.setSound(ringtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
mNotificationManager.notify(getNotificationID(event), builder.build());
mNotificationManager.cancel(getNotificationID(event));
If above code is in your service class. Then I think the problem is when you open app directly instead using notification bar, that time its not calling your service class else you need to call nm.cancel(NOTIFICATION_BAR_ID); from the activity which opens while clicking on notification.
And to do so you need a global class which keeps static NOTIFICATION_BAR_ID, so that will be helpful to you for managing cancel method.
I hope, this will solve your problem.
I'm trying to put my notification on top of notification area.
A solution is to set the parameter "when" to my notification object with a future time like:
notification.when = System.currentTimeMills()*2;
The code that I'm using in this:
long timeNotification = System.currentTimeMillis()*2;
Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notification.when = timeNotification;
notification.priority = Notification.PRIORITY_MAX;
but some apps (like Facebook) are able to put a simple notification with their current time over mine.
If I refresh my notification it remains under these ones.
What parameters I have to set to put my Notification to the top of the notifications area?
You should do this. Other answers seem outdated.
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
setPriority(Notification.PRIORITY_MAX) is important. It can also be replaced with any of the following as per requirement.
Different Priority Levels Info:
PRIORITY_MAX --
Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
PRIORITY_HIGH --
Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
PRIORITY_DEFAULT --
Use for all notifications that don't fall into any of the other priorities described here.
PRIORITY_LOW --
Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.
PRIORITY_MIN --
Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.
For more details check the following link:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority
You can make your notification Ongoing, when it will appear higher then other usual notification. But in this case user would not be able to clear it manually.
In order to do this set flags to your Notification object:
notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR
Try setting priority of the notification to high
documentation > Notification Priority
Also check this question may it could help you Pin Notification to top of notification area
Please note that if you want a "heads-up" notification i.e., one that displays over the top of the current user window you must have the following set in your builder:
setDefaults(NotificationCompat.DEFAULT_VIBRATE)
The reference is in the javadoc:
A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms.
Complete example for a heads-up notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
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"