为什么在 startForeground 中 "setOngoing" 或 "setAutoCancel" 不起作用?

Why Doesn't Work "setOngoing" or "setAutoCancel" in startForeground?

我正在做一个 android 项目。我想在应用程序被杀死时发送通知。我做到了,但是,当向左滑动或单击通知时,我无法删除通知。我尝试使用

.setOngoing(false)

.setAutoCancel(true).

但是,它对我不起作用。

 Intent notificationIntent = new Intent(Service.this, Message.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(Service.this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(Service.this, CHANNEL_ID)
                .setContentTitle("Message")
                .setSmallIcon(R.drawable.message)
                .setContentIntent(pendingIntent)
                .setOngoing(false)
                .setAutoCancel(true)
                .build();

        startForeground(1, notification);

我使用 NotificationCompat.Builder 时成功,但 startForeground 不接受。我能做什么?

In Order to cancel or swipe foreground notification

.setOngoing(false).setAutoCancel(true)

You will have to kill the Service using `stopService()

注意:- 不允许用户滑走正在进行的前台服务生成的通知。