Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heads Up Notification getting dismissed after few second

I am trying to make a heads up notification through the code below and want it to to be persistent until the user decide to dismiss it. But it is getting dismissed automatically after few second (around 10 sec). Is there any way to make it persistent and leave it to the user to dismiss it.

NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setContentText("Hello !!!")
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher,
"View Call", null)
.addAction(R.drawable.ic_launcher,
"Call Back", null);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
like image 755
kaddy Avatar asked Oct 22 '25 06:10

kaddy


1 Answers

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);
like image 174
jad Avatar answered Oct 23 '25 22:10

jad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!