Java 无法清除通知并从通知中打开片段

Java 无法清除通知并从通知中打开片段,java,android,android-studio,notifications,fragment,Java,Android,Android Studio,Notifications,Fragment,我正试图清除通知。但它仍然存在。无法从通知打开片段活动。下面是我的代码 @Override public int onStartCommand(Intent intent, int flags, int startId) { int sticky; try { AndroidLogger.log(5, TAG, "Missed call notification on start"); if (Build

我正试图清除通知。但它仍然存在。无法从通知打开片段活动。下面是我的代码

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int sticky;

        try {
            AndroidLogger.log(5, TAG, "Missed call notification on start");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
                NotificationChannel channel = new NotificationChannel("1", "Call Notification", importance);
                channel.setDescription("Get alert for missed call");
                channel.setShowBadge(true);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
            Intent notifyIntent = new Intent(this, ViewFragment.class);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
                    .setSmallIcon(R.drawable.nexge_logo)
                    .setContentTitle("Missed Call")
                    .setContentText(intent.getStringExtra("Number"))
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_MAX);

            Notification buildNotification = mBuilder.build();
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //mNotifyMgr.notify(1, buildNotification);

            startForeground(1,buildNotification);
        } catch (Exception exception) {
            AndroidLogger.error(1, TAG, "Exception while starting service", exception);
        }
        return START_NOT_STICKY;
    }
}

谁能帮我解决这个问题。提前谢谢。 下面是我的另一个问题,我没有得到正确的答案。你也帮我吧

我对类似问题的解决方案是将PendingEvent标志更改为PendingEvent.Flag\u ONE\u SHOT 来自Android文档:

指示此PendingEvent只能使用一次的标志。用于getActivity(Context,int,Intent,int)、getBroadcast(Context,int,Intent,int)和getService(Context,int,Intent,int)

如果设置了,则在对其调用send()后,它将自动取消,并且将来通过它发送的任何尝试都将失败

以及添加通知标志\u自动\u取消标志:

mBuilder.flags |=Notification.FLAG_AUTO_CANCEL

这将确保通知在使用后将被删除

编辑: 应该打电话

Notification notification = mBuilder.build();
首先,然后

notification.flags = Notification.FLAG_AUTO_CANCEL;
Edit2: 刚刚注意到您正在为
startForeground()
使用通知。这意味着只要服务/活动正在运行,通知就会一直保留(这是默认情况,因此用户将知道仍有服务/活动在运行)


只要您的服务/活动作为前台服务运行,通知就会一直保留。

请加入此群并ping我,我会在那里解释。@Ali我没有足够的声誉在聊天室中交谈您有兄弟,您只需等待管理员接受您的请求。你发送加入请求了吗?好的,忘了团体了吗。你的问题是,如果你点击通知,那么碎片没有加载对吗?@Ali ya没错,还有一件事是在我的代码中向右滑动后无法清除顶部栏上的通知我还有两个频道…当我打开我的应用程序时,此时,在顶部栏中会显示一条通知,如提及此应用程序正在运行。只有当应用程序关闭时,它才会消失。。这是否会导致我创建的通知未被清除???基本上是的。由于您提到了另外两个频道,它们在您发布的代码中不可见,我不能肯定地告诉您,但是如果您只想显示一个带有某些附加意图的单次显示通知,您必须使用“mNotifyMgr.notify(1,buildNotification)”而不是“startForeground(1,buildNotification)”来显示它我尝试过使用“mNotifyMgr.notify(1,buildNotification)”。但一切都没有改变,我得到了一个类似RemoteException的异常