Skip to content Skip to sidebar Skip to footer

Is There A Way To Keep Notification Active Until User Clicks Clear?

I have a service that runs in the background and notifies the user if they have a new PDF to view. When they click the notification a windows opens up giving them the option to dow

Solution 1:

For API below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:

// Create notificationNotificationnote=newNotification(R.drawable.your_icon, "Example ", System.currentTimeMillis());

// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);

// THIS LINE IS THE IMPORTANT ONE            // This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;

For API levels above 11, or when using the Android Support Library, one can implement it like this:

Notificationnoti=newNotification.Builder(mContext)
    .setContentTitle("title")
    .setContentText("content")
    .setSmallIcon(R.drawable.yourIcon)
    .setLargeIcon(R.drawable.yourBigIcon)
    .setOngoing(true) // Again, THIS is the important line. This method lets the notification to stay.
    .build();

or you can use NotificationCompat class.

public NotificationCompat.Builder setAutoCancel(boolean autoCancel)

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.

Post a Comment for "Is There A Way To Keep Notification Active Until User Clicks Clear?"