簡體   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