I have up to 4 separate alarms. When an alarm goes off, I display a status bar notification. If I have 2 alarms go off at the same time, I want to have only 1 status bar icon. If I cancel one of the two alarms, I still want to see that single status bar icon (since there is still an alarm going off, even though I cancelled the first alarm). When I cancel the last alarm on the screen I want to remove the status bar icon.
Is there a built in way to do this or do I have to keep track of what alarms are on the screen and only dismiss the notification if it's the last alarm?
Thanks for your help.
What I understood that you want only latest notification to be displayed and if you cancel your last alarm all should go. Here is my solution:
Call the same notification function every time you set notification as it will replace the previous one.
Pass an argument to notification function in case you want to cancel the notification.
If a notification is alredy there and now you want to cancel the next alarm and you want the previous notification should be there, then you should maintain a variable in sharedpreference which will tell you not to cancel the previous notification.You should check this before calling notification function
When you cancel the last alarm then you can pass the argument to notification function as you cancel any other alarm.
Related
I have an ongoing task and after it finishes, I want the notification to become cancelable.
For that, I create a new notification with notification.flags as zero, but the notification keeps being un-cancelable.
Apparently, FLAG_FOREGROUND_SERVICE prevented the notification from turning into cancelable, even after reseting the flags.
After removing FLAG_FOREGROUND_SERVICE and using only 0 or FLAG_ONGOING_EVENT, the notification could be made cancelable or un-cancelable - respectively.
Please take a look here : Android update notification
In short what you will do is this:
Create your notification first time and assign a notification ID to it.
Once your service is done executing create a new cancelable notification with the same id
fire that notification, it should make the previous notification cancelable .
I don't know if this can be done but I have a scheduler/diary app that allows the user to add/remove items. When the user adds an item, they are given an option to set a notification for either 1 hour, 30 mins, or 15 mins before the event time or else to not be notified. I am using an alarm manager with a broadcast receiver to handle this.
I want to display an option in the app menu to enable/disable notifications; i.e. if an alarm is scheduled to sound in 15 mins it should not happen. How can I implement this?
Take a look at NotificationManager class. When you set your notification you can provide a notification id like this:
mNotificationManager.notify(id, notification)
If you later want to cancel this notification(i.e. like in your case, if a user has selected an option to disable notifications) you would need to use
mNotificationManager.cancel(id)
Or you could use cancelAll() to cancel all notifications:
mNotificationManager.cancelAll ()
Let me know if this helps.
I would like to animate the icon of the app without having to cancel the notification and create a new one (because in this way the icon doesn't stay in the same position of the notification bar but could move to first place if there are other notification running).
I'm able to get this with normal notification, but I would like to get the same behaviour when I use startForeground in my service. This method launches a new notification which can't be removed unless you remove service from foreground using stopForeground.
Is this possible to do? How?
Use the same notification ID for the startForeground method and the Notification object.
I am currently making an application that uses Notifications.
I was able to display the notifications, and remove them from the notification list when the user taps them. However, I would also like my notifications to disappear if the user sees them but does not act on them.
For instance, the user displays the notification list, then taps on a notification that is not mine, or just closes the notification list. During those cases, I am trying to make my notifications get canceled and not displayed the next time the user displays the notification list even if he did not do anything to my notification.
Is this possible?
Thanks! :D
(edit: if you are wondering why I thought of this, a very simplified explanation would be: think of the notification as a toast instead; a toast that has a longer existence, and makes sure that the user actually saw it before disappearing.)
you may remove a notification by providing its id to the cancel method:
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(your_notification);
however, i wouldn't recommend you to do it as it may confuse the user
It is very easy, there is a cancel method for notifications:
public void cancel (int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancel (String tag, int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancelAll ()
Cancel all previously shown notifications. See cancel(int) for the detailed behavior.
Details are here:
http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
Update
If you want to cancel your notification when user sees that notification, there is a flag for that, FLAG_AUTO_CANCEL:
Notification notification1 = new Notification(R.drawable.icon, "test",
System.currentTimeMillis());
notification1.flags |= Notification.FLAG_AUTO_CANCEL;
I want to set a onClickListner for a status bar Notification. How it is possible ? Please help. Now i can load a Activity by using the pending intent. I like to set a onClickListner for the notificatioin.
Regards
Parvathi
It is not possible to set an OnClickListener for a notification. Because of the way notifications are handled/displayed there is no way to guarantee that the process that created the notification will be running at the time the notification is clicked. This means that any code you wrote to provide click handling may not be running.
If you need click listener style behavior you will have to do it using the PendingIntent: set it to start a Service that runs the logic or to use an Intent that is received by a BroadcastReceiver. This will let you perform activity without requiring a UI.