notification.setOngoing(true)在Android 8.1中无法正常工作

3
 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);
            }

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

我不知道我错在哪里。

根据文档

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呢?


我在vivo手机上也遇到了同样的问题,你解决了吗? - coderGtm
@coderGtm,正如Raymond在回答中所说,不要浪费时间为每个可能的手机解决问题。但很奇怪,自2018年以来,ColorOS将会有许多更新,应该会被修复。 - Ashvin solanki
我发现的一个解决方法是每隔3或4秒钟使用相同的通知ID通知用户。这样,即使用户能够关闭通知,它也会重新出现。 - coderGtm
1个回答

10

简而言之,这是由于碎片化和定制化导致的。可能该手机有某种设置来配置此行为。正如有人曾经说过:有些手机只是“垃圾”。不要浪费时间尝试为每个可能的手机解决问题。


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