java - notification.setOngoing(true) 在 Android 8.1 中不起作用

标签 java android operating-system android-notifications android-8.0-oreo

 build.setOngoing(true);
 build.setAutoCancel(false);
 notification.flags= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

setOngoing 不工作

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                /* Create or update. */
                 _notificationChannel = new NotificationChannel("TubeMateGo",
                        "Downlaod File Notification",
                        NotificationManager.IMPORTANCE_DEFAULT);
                mNotifyManager.createNotificationChannel(_notificationChannel);
                build = new NotificationCompat.Builder(getBaseContext(),_notificationChannel.getId());
                build.setContentTitle("Download")
                        .setContentText("Download in progress")
                        .setSmallIcon(R.drawable.ic_launcher_foreground);
                build.setOngoing(true);
                build.setChannelId(_notificationChannel.getId());
                build.setVisibility(Notification.VISIBILITY_PUBLIC);
                build.setAutoCancel(false);

                build.setContentIntent(_pedingIntent);

                Notification notification = build.build();
                notification.flags= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
                mNotifyManager.notify(id, notification);
            }
            else {
                build = new NotificationCompat.Builder(getBaseContext());
                build.setContentTitle("Download")
                        .setContentText("Download in progress")
                        .setSmallIcon(R.drawable.ic_launcher_foreground);
                build.setOngoing(true);
                build.setVisibility(Notification.VISIBILITY_PUBLIC);

                Notification notification = build.build();
                notification.flags=Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
                mNotifyManager.notify(id, notification);
            }

当我尝试从通知栏中删除通知时,为什么会删除?

我不知道我哪里错了

According to Docs

public Notification.Builder setOngoing (boolean ongoing)

Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).

那为什么会被用户拒绝呢?

编辑 2

这会发生在自定义操作系统中吗?喜欢Vivo

测试 - RedMI note 5 Pro - 工作正常 那么为什么 ?不使用 Vivo?

最佳答案

简而言之,这是由于 fragment 化和定制化造成的。 可能该电话具有某种设置来配置该行为。正如某人有一天所说:有些手机只是“垃圾”。不要浪费时间尝试为每部可能的手机解决问题。

关于java - notification.setOngoing(true) 在 Android 8.1 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043991/

相关文章:

java - 如何在 spring-jpa 中使用 @GenerateValue 获取序列中的下一个值

java - Android mutableListof(pair(k,v) --> 如果我清除变量,pair 值为空,但我在clear() 之前添加 mutableListof...为什么会为空?

c - AH=2的BIOS INT 13H每次只能读取72个扇区。为什么?

windows - 如何休眠应用程序?

java - 如何在浏览器中获取旧格式的 MediaWiki API 输出?

java - 如何创建和编写包含 "Document"(XOM) 内容的 XML

java - 如何从静态方法调用非静态方法

Android imageview 色调

java - Android必须实现继承的抽象方法

python - 命令提示符在命令后关闭?如何阻止它?