Disable clearing notification for my application

I need to write some kind of application that will work in the background. This application will start from autorun, and there will be no launch. Gui can be invoked by clicking on a notification that will be displayed using autorun. I was afraid that when the user cleared the notifications, he lost the ability to name this gui. My question is, is there a way to block the user from clearing my notification?

+6
source share
5 answers

You want to implement Foreground Service .

-4
source

A notification will appear here that will not 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(); 

In the call to setOngoing(true) this happens, and setAutoCancel(false) stops the notification when the user deletes the notification.

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

+17
source

You might want to view the notification in the "Execution" section of notifications. These notifications are not cleared when the user clears them.

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

+4
source

Although @cja's answer may be right, although it lacks a few lines of code (a notification does not appear or will not appear in your notification tray).

This is the full 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 int is your notification identifier.

you can clean it using this:

 public void clear() { NotificationManager oldNoti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); oldNoti.cancel( NotificationID ); } 

make sure for notification.setAutoCancel( false ); set to false , so it will not be cleared when you click the clear button or if there are transfer gestures.

multiple lines of code are initially sent from the @cja message.

greetings / happy encodings ...

+1
source

Just add these two flags to the notification, FLAG_AUTO_CANCEL will prevent the notification from automatically rejecting when the user touches it, and FLAG_ONGOING_EVENT will make it “Permanent notification” .

 notification.flags=Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONGOING_EVENT; 
0
source

Source: https://habr.com/ru/post/894437/


All Articles