Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss Heads Up notification and create a normal one

I'm using this code to create a Heads Up notification.

private static void showNotificationNew(final Context context,final String title,final String message,final Intent intent, final int notificationId, final boolean isHeaderNotification) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
            .setSmallIcon(R.drawable.prime_builder_icon)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_MESSAGE)
            .setContentTitle(title)
            .setContentText(message)
            .setWhen(0)
            .setTicker(context.getString(R.string.app_name));

    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    notificationBuilder.setContentText(message);
    if(isHeaderNotification) {
        notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, false);
    }

    notificationBuilder.setContentIntent(fullScreenPendingIntent);
    notificationBuilder.setAutoCancel(true);


    Notification notification = notificationBuilder.build();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notificationId, notification);
}

The thing is, notification should appear occupying a good part of top screen to call user attention, but after a couple seconds it should dismiss and a normal notification should appear.

But this code doesn't do that. The notification stay occupying all the top screen until user dismiss it.

I'm thinking in create another normal notification with same ID after a couple of seconds using Handler, but I want to know if there is a better way to do this.

Follow an example of WhatsApp, simulating the behavior I want.

enter image description here enter image description here

like image 860
leonvian Avatar asked Feb 08 '23 10:02

leonvian


1 Answers

The issue is caused because you use setFullScreenIntent:

An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time. If this facility is used for something else, please give the user an option to turn it off and use a normal notification, as this can be extremely disruptive.

Also as explained in this answer you should use setVibrate to make Heads-up work.

This is an example of working Heads-up notification:

private static void showNotificationNew(final Context context, final String title, final String message, final Intent intent, final int notificationId) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
            .setSmallIcon(R.drawable.small_icon)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentTitle(title)
            .setContentText(message)
            .setVibrate(new long[0])
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notificationId, notificationBuilder.build());
}
like image 125
Mattia Maestrini Avatar answered Feb 15 '23 09:02

Mattia Maestrini