没有“悬浮通知”的通知

4

我正在尝试创建一个高优先级的通知(一个聊天应用),但我的客户要求它不会有"悬浮窗"视图。

我尝试创建一个空布局用作RemoteViews,并将其设置为Notification.headsUpContentView,但仍然没有效果。

这是我尝试过的:

    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" />
2个回答

1
显示悬浮通知是高优先级通知的定义的一部分:
如果通知的优先级被标记为高、最高或全屏,它将得到一个悬浮通知。
如果您不想要悬浮通知,则不需要高优先级通知。
请注意,用户可以选择禁用悬浮通知或降低您的通知的优先级,但这仅适用于用户自己。

1
如果您坚持使用 PRIORITY_MAX,则可以在API 21及以上版本中使用以下代码禁用悬浮通知:

notification.headsUpContentView = new RemoteViews(Parcel.obtain());

1
当添加了这个功能后,通知停止显示,在Android Pie 9.0上进行了测试。 - Rohan Kandwal
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" 而且通知也没有显示出来。 - Sergey Galin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接