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?
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)
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
Though @cja answer might be correct though he is missing some few lines of codes(Notification wont show or wont display on your notification tray).
This is the complete working function :
public void createNotification() {
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
notification.setTicker( "Ticker Text" );
notification.setSmallIcon(R.drawable.ic_launcher);
notification.setContentTitle( "Content Title" );
notification.setContentText( "Content Text" );
notification.setAutoCancel( false );
notification.setOngoing( true );
notification.setNumber( ++NotificationCount );
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setContentIntent(pIntent);
notification.build();
NotificationManager nManger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManger.notify(NotificationID, notification.build());
}
NotificationID is int serve as your notification's ID.
you can clear it by using this :
public void clear() {
NotificationManager oldNoti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
oldNoti.cancel( NotificationID );
}
make sure that notification.setAutoCancel( false );
is set to false so it wont be cleared when clear button is pressed or when swipe gesture is present.
few lines of codes are originally from @cja post.
cheers / happy codings...
Just add these two flags to the notification, FLAG_AUTO_CANCEL prevent notification automatically dismissed when the user touches it and FLAG_ONGOING_EVENT make it an Ongoing Notification.
notification.flags=Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONGOING_EVENT;
using this alone works perfectly for me Notification.FLAG_NO_CLEAR along with notification.SetOngoing(true);
notification.SetAutoCancel(false);
You want to implement a Foreground Service.
精彩评论