2014-09-19 105 views
1

我可以通過點擊通知刪除此通知嗎?如何在點擊通知時刪除通知android

當使用setAutoCancel(true)單擊此通知時,我可以取消通知。

但點擊通知後不清楚通知。

private void sendNotification(String msg, Bundle extras) { 
    notificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent intent; 
    if (extras != null) { 
     intent = new Intent(this, MessageConversationActivity.class); 
     intent.putExtra("sender_id", extras.get("sender_id").toString()); 
     intent.putExtra("receiver_id", Application.ACTIVE_USER.getUser_id()); 
     intent.putExtra("gender", extras.get("gender").toString()); 
     intent.putExtra("profile_picture", extras.get("profile_picture") 
       .toString()); 
     intent.putExtra("username", extras.get("username").toString()); 
    } else { 
     intent = new Intent(this, MainActivity.class); 
    } 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      intent, 0); 

    String app_name = getString(R.string.app_name); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      this).setSmallIcon(R.drawable.app_icon) 
      .setContentTitle(app_name) 
      .setAutoCancel(true) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
      .setContentText(msg); 

    builder.setContentIntent(contentIntent); 
    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

回答

1

嘗試這個 builder.setAutoCancel(true);

-1

您可以通過使用

Notification.FLAG_AUTO_CANCEL 

然後開始頁面的意圖你想

0

如果您的通知設置要做到這一點自動取消一旦用戶選擇了它,它也將被刪除。

OR

您也可以撥打取消()爲特定的通知ID取消通知。

notificationManager.cancel(notifyId); 

MessageConversationActivity類中使用onCreate方法。

0

使用notificationManager.cancel(NOTIFICATION_ID); 它從通知管理器取消通知ID