简体   繁体   English

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

[英]Not able to clear notification and open fragment from notification

I am trying to clear notification.我正在尝试清除通知。 But it remains there.但它仍然存在。 Not able to open fragment Activity from notification.无法从通知中打开片段活动。 Below is my code,下面是我的代码,

 @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;
    }
}

Anybody help me to solve this.任何人都可以帮我解决这个问题。 Thanks in Advance.提前致谢。 Below is my another question for which I didn't get a proper answer.下面是我的另一个问题,我没有得到正确的答案。 Help me with that also About Notification for Missed Call in android也帮我解决关于android中未接来电的通知

My solution to a similar issue has been changing PendingIntent Flag to PendingIntent.FLAG_ONE_SHOT From Android documentation:我对类似问题的解决方案是将 PendingIntent Flag 更改为 PendingIntent.FLAG_ONE_SHOT 从 Android 文档:

Flag indicating that this PendingIntent can be used only once.指示此 PendingIntent 只能使用一次的标志。 For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).与 getActivity(Context, int, Intent, int)、getBroadcast(Context, int, Intent, int) 和 getService(Context, int, Intent, int) 一起使用。

If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.如果设置,在对其调用 send() 之后,它将自动为您取消,并且以后通过它发送的任何尝试都将失败。

and adding notification FLAG_AUTO_CANCEL flag:并添加通知 FLAG_AUTO_CANCEL 标志:

mBuilder.flags |= Notification.FLAG_AUTO_CANCEL;

which should make sure the notification will be removed once used.这应该确保通知将在使用后被删除。

Edit : Should call编辑:应该打电话

Notification notification = mBuilder.build();

first, then首先,然后

notification.flags = Notification.FLAG_AUTO_CANCEL;

Edit2 : Just noticed that you are using the notification for startForeground() . Edit2 :刚刚注意到您正在使用startForeground()的通知。 This means that the notification will stay for as long as your Service / Activity is running (this is by default so the user will know that there is a Service / Activity still running).这意味着只要您的服务/活动正在运行,通知就会一直存在(这是默认设置,因此用户会知道有一个服务/活动仍在运行)。

The notification will stay for as long as your Service / Activity does run as foreground service.只要您的服务/活动确实作为前台服务运行,通知就会一直存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM