4

Is it possible to make a notification automatically disappear after a period of time?

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
  • 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:41

5 Answers5

4

You can use the AlarmManager. I think is more appropriate and more easier to implement than an Android Service.

With AlarmManager you do not need worry about make something running until the time finish. Android do that for you, and send a brodcast when it happen. Your application must have a Receiver to get the correct intent.

Look theses examples:

Community
  • 1
  • 1
Bruno Mateus
  • 1,727
  • 18
  • 25
2

Now there is an option called .setTimeoutAfter(long durationMs)

https://developer.android.com/reference/android/app/Notification.Builder.html#setTimeoutAfter(long)

Sos.
  • 914
  • 10
  • 14
1

Yeah, you can just create a service that runs in the background that'll timeout after five minutes and delete your notification. Whether you "should" actually do that is up for debate. A notification should be there to notify the user... and the user should be able to dismiss it on their own.

From d.android.com:

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

EGHDK
  • 17,818
  • 45
  • 129
  • 204
1

Yeah, it is very easy. Where you get notification there add one handler if notification is not read by user then remove notification.

@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification(message.getData().toString);
}

add notification code

private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("TEST NOTIFICATION")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int id = 0;
        notificationManager.notify(id, notificationBuilder.build());
        removeNotification(id);
    } 

cancel notification code.

private void removeNotification(int id) {
Handler handler = new Handler();
    long delayInMilliseconds = 20000;
    handler.postDelayed(new Runnable() {
        public void run() {
            notificationManager.cancel(id);
        }
    }, delayInMilliseconds);
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
0

You could also use a classic Java Runnable 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

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