Allow users to disable notifications Android - android

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.

Related

Is it possible to remove/cancel a notification after the user sees it?

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;

Group Status Bar Notifications - One Icon

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.

Trigger a notification every x days

I want to trigger a notification in the status bar every x days(that the user will define in the application). How can i code this ?
Thank you.
AlarmManager was designed for stuff like that.
Assuming you already know how to code an android app, you can see these pages for code examples:
To trigger a notification in the status bar: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
To run the trigger evey x days, you can use AlarmManager: http://developer.android.com/reference/android/app/AlarmManager.html

Status bar Notification in android

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.

How to reset notificationmanager number on notification clear

What's the best way to clear the notification number when the user clicks on the notification? I say the best way, but really I haven't found ANY way. I'm launching a built in activity when the user clicks on the notification, not something I wrote so I can't clear it that way. I've got the notification manager's flag set to clear
NotificationManager notification
.
.
.
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number++;
nm.notify(1,notification);
But whatever I do the Notification.number keeps going up and never resets to 0.
You could use an intermediary activity which is part of your app and thus can reset your variable and then start the internal activity.
So the chain would be
Notification --starts--> Intermediary Activity --starts--> Built-in activity
I am using a combination of what #CommonsWare recommends and extending an application object as here How to update notification number .
Edit: Further testing shows that actually this is not working because the BroadcastReceiver is called on every notification and because it reset the number on every notification the number is never correct.

Categories

Resources