11

I am creating an simple android application in that created a notification.

Now i want to remove the notification if the user doesn't response after the particular time

That means i want remove the notification after five mins

Krishna
  • 4,892
  • 18
  • 63
  • 98
  • Possible duplicate of [Clearing notification after a few seconds](https://stackoverflow.com/questions/4994126/clearing-notification-after-a-few-seconds) – mike47 Jul 22 '17 at 18:42

5 Answers5

23

The current correct solution may be to use:

NotificationCompat.Builder#setTimeoutAfter(long)

falvojr
  • 3,060
  • 4
  • 31
  • 54
David
  • 3,971
  • 1
  • 26
  • 65
  • Please read the doc link here people: https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setTimeoutAfter(long) - if you need pre API 26 this will not work – Daniel Wilson Jan 30 '21 at 00:00
6

What you need is a combination of Alarmmanager and notificationmanger.

Register the alarm manager that will call a service in 5 minutes and use NotificationManager.cancel in the service implementation.

Alarm Service Sample is here. I suppose you know to use Notification Manager.

uncaught_exceptions
  • 21,712
  • 4
  • 41
  • 48
2

AlarmManager is more often used for complicated Services that have to run in the Background when the Application is closed. You could also use a classic Java Runnable in a Handler for a simple small Thread.

Handler h = new Handler();
    long delayInMilliseconds = 5000;
    h.postDelayed(new Runnable() {
        public void run() {
            mNotificationManager.cancel(id);
        }
    }, delayInMilliseconds);

Also look here:

Clearing notification after a few seconds

You could also use the TimerTask-Class.

Community
  • 1
  • 1
Paul Oskar Mayer
  • 1,107
  • 1
  • 10
  • 22
1

For Android >= 8.0 (Oreo) we can use this,

NotificationCompat.Builder#setTimeoutAfter(long)

For Android < 8.0 we can use AlarmManager

Add this to AndroidManifest.xml :

<receiver
    android:name=".AutoDismissNotification"/>

Create AutoDismissNotification.kt and add this code :

class AutoDismissNotification : BroadcastReceiver() {

    companion object {
        private const val KEY_EXTRA_NOTIFICATION_ID = "notification_id"
    }

    override fun onReceive(context: Context, intent: Intent) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(intent.getIntExtra(KEY_EXTRA_NOTIFICATION_ID, 0))
    }

    fun setAlarm(context: Context, notificationId: Int, time: Long) {
        val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val alarmIntent = Intent(context, AutoDismissNotification::class.java)
        alarmIntent.putExtra(KEY_EXTRA_NOTIFICATION_ID, notificationId)
        val alarmPendingIntent = PendingIntent.getBroadcast(context, notificationId, alarmIntent, PendingIntent.FLAG_ONE_SHOT)
        alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, alarmPendingIntent)
    }

    fun cancelAlarm(context: Context, notificationId: Int) {
        val alarmIntent = Intent(context, AutoDismissNotification::class.java)
        alarmIntent.putExtra(KEY_EXTRA_NOTIFICATION_ID, notificationId)
        val alarmPendingIntent = PendingIntent.getBroadcast(context, notificationId, alarmIntent, PendingIntent.FLAG_ONE_SHOT)
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.cancel(alarmPendingIntent)
    }
}

When you build the notification, add this :

long timeOut = 5 * 1000L; // Five seconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    builder.setTimeoutAfter(timeOut);
}
else {
    AutoDismissNotification().setAlarm(this, notificationId, timeOut);
}
0

You can create your notification like this.

 NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
                .setSmallIcon(Util.getNotificationIcon(context))
                .setContentTitle(new SessionManager().getDomainPreference(context))
                .setAutoCancel(true)
                .setOngoing(false)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setShowWhen(false)
                .setContentText(summaryText)
                .setTimeoutAfter(3000) // add time in milliseconds
                .setChannelId(CHANNEL_ID);
VIVEK CHOUDHARY
  • 468
  • 5
  • 8