Clearing notification after a few seconds
I don't believe there's a way to use the NotificationManager only to cancel a notification, but you can do it with a simpler Handler. Put some code like this right after you fire your notification.
Handler h = new Handler();
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
mNotificationManager.cancel(YourNotificationId);
}
}, delayInMilliseconds);
Notification not getting cleared after a few seconds. -Android
Maybe check this also
public static int NOTIFICATION = 1;
public void displayNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mCtx, NOTIFICATION,
intent, PendingIntent.FLAG_UPDATE_CURRENT); //FLAG_ONE_SHOT ? FLAG_UPDATE_CURRRENT
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_logo_nobg).setTicker(title).setWhen(0)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_logo_nobg)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_roundedlogo))
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
final NotificationManager notificationManager = (NotificationManager) mCtx
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION, notification);
final int notificationId = NOTIFICATION;
// This method will get rid of the notification AND the message after 1 day
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
notificationManager.cancel(notificationId);
Preference_Manager.getInstance(mCtx).deleteKeyMessage(notificationId);
// Preference_Manager.getInstance(mCtx).deleteKeyMessageid(NOTIFICATION);
}
}, 5000/*howMany */);
}
});
NOTIFICATION++;
}
How to remove the notification after the particular time in android?
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.
Make notification disappear after 5 minutes
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.
android: Show push notification after few seconds on button click?
Try this
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Add your code to display notification here
}
}, 6000);
Notification On/Off after few seconds
function notify(){
setTimeout(function(){
document.getElementById('notificationAddMovie').style.display = 'block'
}, 500);
setTimeout(function(){
document.getElementById('notificationAddMovie').style.display = 'none'
}, 2000);
}
Is that what you need?
Heads Up Notification getting dismissed after few second
This code snippet worked for me.
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piDismiss = PendingIntent.getActivity(this, 0, intent, 0);
//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Ping Notification")
.setContentText("You have a new notification.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_DEFAULT) //must give priority to High, Max which will considered as heads-up notification
.addAction(R.drawable.dismiss,
"View Call", piDismiss)
.addAction(R.drawable.ic_ok,
"ok", null)
.setFullScreenIntent(piDismiss, true);
Related Topics
How to Scroll Recyclerview Programmatically
How to Load Image into Imageview from Url Using Glide V4.0.0Rc1
Resources and Layout Direction Rendered Incorrectly Only on Android 8.0 and Above
How to Create Ripple Effect in Simple Layout
How to Know If a Specific User Has Rated a Android App
Vectordrawablecompat Resources$Notfoundexception on Kitkat and Below
Android: Listview.Getscrolly() - Does It Work
Find the Physical Address of Exception Vector Table from Kernel Module
Android - Activity Not Found Exception
Input Elements on Android 4.X Can Not Be Styled When Focused
@Font-Face Not Working in Chrome for Android
How to Save Image Taken from Camera and Show It to Listview - Crashes with "Illegalstateexception"
How to Apply Primary Key on the Text Fields in Android Database
How to Highlight the Table Row on Click
Listview: Textview with Linkmovementmethod Makes List Item Unclickable
Error: This Android Sdk Requires Android Developer Toolkit Version 22.6.1 or Above
Android Rxjava 2 Junit Test - Getmainlooper in Android.Os.Looper Not Mocked Runtimeexception