Dave Ranjan
Dave Ranjan

Reputation: 2984

How to stop heads-up notification being posted via Android Code?

I am building an android app that replaces the notification drawer and show notifications in its own window. I managed to show notifications on my drawer when they are posted by overriding onNotificationPosted() method. But, same notification is also shown by android. So,I want that notifications should be shown ONLY on my window, there are other apps who have done it,so it's not impossible. Please tell me how to override default behavior of android. Thanks in advance. :)

EDIT

What i want is to disable heads-up notification. Any solutions there?

Upvotes: 3

Views: 8796

Answers (4)

Bear
Bear

Reputation: 11

Set the IMPORTANCE to LOW and PRIORITY to LOW. It work for me

var notificationChannel = NotificationChannel(channelId, "Call" , NotificationManager.IMPORTANCE_LOW)
            var notification = NotificationCompat.Builder(context,channelId)
                    .setContentTitle("Calling")
                    .setContentText("Call")
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_LOW)

Upvotes: 1

rana
rana

Reputation: 1862

In Oreo and above OS It is in the Notification channel Where you have to set the Priority any thing lesser than NotificationManager.IMPORTANCE_HIGH or some thing like this below stops the heads up banner.

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                                   CHANNEL_NAME, 
                                   NotificationManager.IMPORTANCE_DEFAULT)

On Pre-Oreo devices, the notification priority itself helps the system to show/hide the heads up banner. Also remember when you make this change you have to delete the app and reinstall to see the change!

Upvotes: 3

mhcpan
mhcpan

Reputation: 349

I used NotificationManager.IMPORTANCE_DEFAULT with setPriority(NotificationCompat.PRIORITY_DEFAULT) and it seems to work (Oneplus 3 and Note 8).

String channelId = "ch01";
String channelName = "Status";
int importance = NotificationManager.IMPORTANCE_DEFAULT;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(
            channelId, channelName, importance);
    notificationManager.createNotificationChannel(mChannel);
}

NotificationCompat.Builder nb = new NotificationCompat.Builder(this, channelId);
    nb.setSmallIcon(R.drawable.ic_logo);
    nb.setContentTitle(notificationText);
    nb.setContentText("");
    nb.setContentIntent(mainActivityPendingIntent);
    nb.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    nb.setOnlyAlertOnce(true);
    nb.setOngoing(true);
    nb.setWhen(System.currentTimeMillis());

Upvotes: 2

Dave Ranjan
Dave Ranjan

Reputation: 2984

Huh! Looks like Android provides no way to disable heads-up notification of other apps via code.

Here is a little hack!

So, what we need is, not show any heads-up notification from any other app. To solve this, we need to understand that at one time there could be only one heads-up notification on screen.

Hack is, send your own notification right after you listen any notification being posted i.e. in onNotificationPosted() of a NotificationListener subclass.

this.mNotificationManager.notify(12321, new Notification.Builder(this)
                .setContentTitle("").setContentText("")
                .setSmallIcon(R.drawable.transparent)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setFullScreenIntent(this.mBlankIntent, true)
                .setAutoCancel(true)
                .build());

It will replace the notification of the 3rd app with your almost blank notification. Oh wow! Wait!! This looks ugly.

Ofcourse, Now we need to remove this notification before the user could even see this.

Now that you know package name of the android. You can show cancel your notification some thing like this.

if(packageName.equals("com.my.package")){
        mNotificationManager.cancel(12321);
        return;
    }

So, what we are doing is showing our notification, when any other heads up notification shows up, and then remove our own notification. All this happens so quick that user does not sees any heads-up notification, and for doing this, I did not needed to store the notification in memory even.

Upvotes: 1

Related Questions