Jame
Jame

Reputation: 3854

Is it possible to reduce notification time showing in Android?

I am using bellow code to show a notification on my Android phone. I want to reduce the time which uses to shows the notification. Now, it spends about 2 seconds for showing, then it is hidden. I want to reduce the time from 2 seconds to 1 second. Is it possible?

Notification notification = new NotificationCompat.Builder(context)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentText("Notification")
                    .setAutoCancel(false)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .build();

NotificationManager notificationManager = getNotificationManager(context);
notificationManager.notify(SAFER_DISCONNECTED_NOTIFICATION_ID, notification);

Upvotes: 5

Views: 2681

Answers (2)

Misagh
Misagh

Reputation: 3623

Notifications are shown by android, and there are no permissions for applications for changing theirs notifications preview time-life in non rooted devices.

But if the content of the notifications are not important, you can cancel notifications programmatically after one second:

1- create a time for doing your task after one second:

new java.util.Timer().schedule(
    new java.util.TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new   Runnable() {
                public void run() {
                    //your code for cancel notification(s)
                }
           });
        }
    }, 
1000);

2- you can cancel a notification:

NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(NOTIF_ID);

or cancel all notifications in given context:

nMgr.cancelAll();

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006654

Now, it spends about 2 seconds for showing, then it is hidden.

There is no particular guarantee regarding the amount of time that a Notification will be shown in heads-up mode, which I assume is what you are referring to.

For example, by default it will be shown for 0 seconds on Android 4.4 and earlier, which does not natively support heads-up notifications. However, a user could install an app with a NotificationListenerService, and that service could display the notification however, and for however long, it wants.

Is it possible?

No, sorry. That is under the control of the code that displays the notifications.

Upvotes: 2

Related Questions