【问题标题】:Not able to clear notification and open fragment from notification无法清除通知并从通知中打开片段
【发布时间】:2020-01-08 08:50:27
【问题描述】:

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

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

谁能帮我解决这个问题。提前致谢。 以下是我没有得到正确答案的另一个问题。也帮助我 About Notification for Missed Call in android

【问题讨论】:

  • 请加入这个群Android & Kotlin Experts并联系我,我会在那里解释。
  • @Ali 我没有足够的声望来聊天
  • 您有兄弟,您只需等待管理员接受您的请求。您发送加入请求?
  • 好吧,忘记组了。你的问题是如果你点击通知然后片段加载不正确?
  • @Ali ya 没错,还有一件事是右滑后无法清除顶部栏上的通知

标签: java android android-studio notifications fragment


【解决方案1】:

我对类似问题的解决方案是将 PendingIntent 标志更改为 PendingIntent.FLAG_ONE_SHOT 来自 Android 文档:

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

如果设置,则在调用 send() 后,它将自动为您取消,以后任何通过它发送的尝试都将失败。

并添加通知 FLAG_AUTO_CANCEL 标志:

mBuilder.flags |= Notification.FLAG_AUTO_CANCEL;

这应该确保通知一旦使用就会被删除。

编辑: 应该调用

Notification notification = mBuilder.build();

先,然后

notification.flags = Notification.FLAG_AUTO_CANCEL;

编辑2: 刚刚注意到您正在使用startForeground() 的通知。这意味着只要您的服务/活动正在运行,通知就会一直存在(这是默认设置,因此用户会知道有服务/活动仍在运行)。

只要您的服务/活动确实作为前台服务运行,通知就会一直存在。

【讨论】:

  • 在我的代码中我还有两个频道...当我打开我的应用程序时,当时在顶部栏中会显示一个通知,就像提到这个应用程序正在运行一样。只有当应用程序关闭时才会消失。这是否会导致我创建的通知不被清除???
  • 基本上,是的。由于您提到了在您发布的代码中不可见的另外两个频道,我不能肯定地告诉您,但是如果您只想显示一个带有某些意图的单显示通知,您必须显示它使用“mNotifyMgr.notify(1, buildNotification)”而不是“startForeground(1, buildNotification)”
  • 我尝试过使用“mNotifyMgr.notify(1, buildNotification)”。但没有任何改变,我得到了 RemoteException 之类的异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多