单击后,从通知区域中删除通知

时间:2012-12-13 05:44:23

标签: android android-notifications

每当收到包含特定关键字的新邮件时,我都会显示通知。我使用以下代码在通知区域中显示通知,

String contentTitle = "V-Card Received"; 
String contentText = "You have reeived a new V-Card"; 
 mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, receiveVCard.class);
 notificationIntent.putExtra("sender", sender);
notificationIntent.putExtra("vCardString", messages[i].getDisplayMessageBody());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
int icon = R.drawable.contactvcard;
CharSequence tickerText = "V-Card Received";
long when = System.currentTimeMillis();
notifyDetails = new Notification(icon, tickerText, when);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 
notifyDetails.flags =Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

现在,我想在用户点击通知后删除该通知。我已使用Notification.FLAG_AUTO_CANCEL取消通知。但即使用户点击它也不会删除通知。当用户点击通知时,是否有其他方法可以删除通知。

4 个答案:

答案 0 :(得分:2)

通常在放置通知后设置标志。

您需要交换您提供的代码的最后两行。在调用nm.notify();

之前设置标志

答案 1 :(得分:1)

试试这个对我来说很好用

--> notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
--> notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notificationManager.notify(i, notification);

答案 2 :(得分:1)

这是我在我的某个应用中使用的通知的原型

    Notification notification=new Notification(R.drawable.ic_stat_download_interrupted,getResources().getString(R.string.dint),System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_download_complete);
    contentView.setImageViewResource(R.id.notimage, R.drawable.ic_stat_download_interrupted);
    contentView.setTextViewText(R.id.nottext, getResources().getString(R.string.dint));
    contentView.setTextViewText(R.id.nottitle, update.initialDetail.fileName);

    notification.contentView = contentView;      
    notification.flags=Notification.FLAG_AUTO_CANCEL;
    Intent notificationIntent = new Intent(getApplicationContext(), MainDashboard.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra(EXTRA_NOTIFICATION_SHOW_DOWNLOADS, true);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,notificationIntent, 0);
    notification.contentIntent=contentIntent;

nm.notify(update.updateId.intValue(), notification);

答案 3 :(得分:0)

为我工作

notifyDetails.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;

希望这对你有所帮助。