kaddy
kaddy

Reputation: 119

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());

Upvotes: 4

Views: 2080

Answers (1)

jad
jad

Reputation: 493

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);

Upvotes: 4

Related Questions