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
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.
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.
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);
}
The current correct solution may be to use:
NotificationCompat.Builder#setTimeoutAfter(long)
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);