0

I need heads-up notification which will be not cancelable from notification drawer so I created Heads-up Ongoing notification:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.logo_notification)
                    .setContentTitle("Title")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setGroup(KEY)
                    .setGroupSummary(true)
                    .setOngoing(true)
                    .setColor(context.getResources().getColor(R.color.tab_indicator_color));


    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, Activity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(WActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent pIntent = new Intent(context, PService.class);
    pIntent.setAction("ACTION");
    PendingIntent piPause = PendingIntent.getService(context, NOTIFICATION_ID, pIntent, 0);
    mBuilder.addAction(icon, title, piPause);

    Intent sIntent = new Intent(context, SService.class);
    sIntent.setAction("ACTION");
    PendingIntent piDismiss = PendingIntent.getService(context, NOTIFICATION_ID, sIntent, 0);
    mBuilder.addAction(icon2, title2, piDismiss);

The problem is that After notification shows on the top of screen it does not hide to notification drawer. But if notification is not ongoing, it hides. I need heads-up notification which will hide to notification drawer.

sofi37
  • 323
  • 1
  • 4
  • 15

1 Answers1

-1

AFAIK, a notification needs the following properties to be set to be shown as a heads-up notification.

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)

setOngoing() means that the Notification is ongoing and cannot be hidden by the user. I believe that is probably why your Notification stays on the top and doesn't hide. Remove setOngoing(true) and it should work.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • 1
    But I need notification which will be not cancelable from list of notifications(notification drawer) but will hide from top of the screen after few seconds. – sofi37 May 18 '16 at 11:48
  • For that you can use these flags `mNotification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;` – Swayam May 18 '16 at 11:50
  • I tried it but the notification still stay on the top of screen and hot hide to notification list – sofi37 May 18 '16 at 12:06
  • Have you find any solution? – vineet pal Dec 29 '22 at 11:09