Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable clearing notification for my application

I need to write some application which will do some work in background. This application will be run from autostart and there wont be any start gui. Gui can be call from click on notification which will be showing with autostart. I worried that, when user clear notifications he lost opportunity to call this gui. My question is that is there any way to block clearing my notification by user?

like image 208
klemens Avatar asked Aug 06 '11 10:08

klemens


2 Answers

Here's a notification that won't allow the user to clear it.

Notification notification = new NotificationCompat.Builder(this)
        .setTicker(r.getString(R.string.app_name))
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(r.getString(R.string.app_name))
        .setAutoCancel(false)
        .setOngoing(true)
        .build();

The setOngoing(true) call achieves this, and setAutoCancel(false) stops the notification from going away when the user taps the notification.

The notification will be cleared if the application is uninstalled or by calling Cancel or CancelAll: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

like image 186
cja Avatar answered Oct 18 '22 14:10

cja


You might want to look into making a notification in the "running" section of the notifications. These notifications aren't cleared when the user clears them.

Use the Notification.FLAG_NO_CLEAR AND Notification.FLAG_ONGOING_EVENT. This should give you the effect you want

like image 29
Kurru Avatar answered Oct 18 '22 16:10

Kurru