4

I am trying to create a notification that has a high priority (a chatting app) but my client requested that there will be no "heads up" view for it.

I have tried creating an empty layout for a RemoteViews to set as the Notification.headsUpContentView but still nothing.

Here is what I've tried:

    Intent target = new Intent(this, PushConsumedBroadcastReceiver.class);
    target.putExtra(PushConsumedBroadcastReceiver.PUSH_TYPE, PushConsumedBroadcastReceiver.PUSH_TYPE_REMINDER);

    PendingIntent pending = PendingIntent.getBroadcast(this, Constants.REQUEST_CODE_BASE, target, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this).setContentIntent(pending)
    .setContentTitle(item.getNotificationText())
    .setStyle(new BigTextStyle().bigText(item.getNotificationText()).setSummaryText("thepoosh looks good"))
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
    .setSmallIcon(R.drawable.g_icon_white)
    .setPriority(Notification.PRIORITY_MAX)
    .setTicker(item.getNotificationText())
    .build();

    if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
        notification.headsUpContentView = new RemoteViews(getPackageName(), R.layout.empty_heads_up);
    }
    notification.sound = null;
    notification.ledARGB = 0;
    notification.vibrate = new long[0];

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(CODE_ONE_DAY_REMINDER, notification);

empty_heads_up.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />
thepoosh
  • 12,497
  • 15
  • 73
  • 132

2 Answers2

1

Displaying a heads up notification is a part of the definition of a high-priority notification:

If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.

If you don't want a heads-up notification, you do not want a high-priority notification.

Note that you users can opt to disable heads up notifications or lower the priority of your notifications themselves, but this is an option solely for the user.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
1

If you insist on using PRIORITY_MAX, you can disable the Heads-Up notification using the following code starting with API 21:

notification.headsUpContentView = new RemoteViews(Parcel.obtain());
Daniels Šatcs
  • 526
  • 6
  • 18
  • 1
    The notifications stops showing when this is added, tested on Android Pie 9.0 – Rohan Kandwal Oct 25 '18 at 05:16
  • Android 10: "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.pm.ApplicationInfo.writeToParcel(android.os.Parcel, int)' on a null object reference" And the notification does not show up all. – Sergey Galin Jan 20 '20 at 12:10