notification's vibrate and sound defaults are working as INSISTENT - android

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.

Related

Headsup Notification Play Sound Over Other Notification

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).

Sound played with FLAG_INSISTENT notifications don't stop when my app cancels the notification

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.

Overwrite existing LED notification

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 to use notification with sound and vibration?

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)

make android notification stay until user clears it

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.

Categories

Resources