繁体   English   中英

Android Notification在点击/滑动时不会被解雇

[英]Android Notification not getting dismissed on click/swipe

我正在使用startForeground(id,notification)从intent服务创建通知。

Random r=new Random();
int id=r.nextInt(9999);

Builder notice2=new Notification.Builder(getApplicationContext())
    .setContentTitle(call.getName())
    .setAutoCancel(true)
    .setContentIntent(intent)
    .setContentText("content")
    .setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));

startForeground(id, notice2.getNotification());

这里我设置了AutoCancel(true)

但是当我点击通知时它不会消失?

我真的很困惑。 我已经尝试过几个小时但仍然没有运气!

请帮忙!

谢谢!

我从另一篇文章的答案中找到了答案。 基本上只有一个前台服务。 使用startForeground()生成通知意味着只要此服务正在运行,就无法删除通知。

而是使用NotificationManager.notify()只生成通知。 为此通知设置AutoCancel(true)会使其在滑动/单击时消失。

谢谢!

您可以像这样修改代码,以便在单击时取消Notification

Random r=new Random();
int id=r.nextInt(9999);

Builder notice2=new Notification.Builder(getApplicationContext())
    .setContentTitle(call.getName())
    .setAutoCancel(true)
    .setContentIntent((PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT))
    .setContentText("content")
    .setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));

startForeground(id, notice2.getNotification());

我没有使用简单的普通Intent ,而是使用PendingIntent和相应的Flag设置来取消当前的Notification
以下是有关PendingIntent一些信息性链接:

  1. http://developer.android.com/reference/android/app/PendingIntent.html
  2. 什么是Android PendingIntent?

我希望这有帮助。

当服务仍在前台时,AutoCancel不起作用。 要允许通过滑动关闭,必须调用stopForeground():

startForeground(id, notice2.getNotification());
stopForeground(false); //false - do not remove generated notification

暂无
暂无

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

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