Android 单击通知区域后将其从通知区域中删除

Android 单击通知区域后将其从通知区域中删除,android,android-notifications,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 no

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

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;

现在,我想在用户单击通知后将其删除。我使用了
通知。FLAG\u AUTO\u CANCEL
取消通知。但是,即使用户单击通知,它也不会删除该通知。当用户单击通知时,是否有其他方法可以删除通知。

试试这个,对我来说效果很好

--> 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);

试试这个对我来说很好

--> 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);

这是我在我的一个应用程序中使用的通知的原型

    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);

这是我在我的一个应用程序中使用的通知的原型

    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);
它对我有用

notifyDetails.flags=Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL

希望这对你有帮助。

它对我有用

notifyDetails.flags=Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL


希望这对您有所帮助。

您基本上是在发出通知后设置标志


您需要交换您提供的最后两行代码。在调用nm.notify()之前设置标志

您基本上是在发出通知后设置标志


您需要交换您提供的最后两行代码。在调用nm.notify()之前设置标志

也可以使用setAutoCancel()方法。也可以使用setAutoCancel()方法。是的,我可以看到,但是试着用我的代码特殊行替换你的代码,所有行都用-->符号标记…它不起作用。下次收到新通知时,它将删除该通知。是的,我可以看到,但请尝试用我的代码专用行替换您的代码,所有行均标记为-->符号…..它不起作用。下次收到新的通知时,它将删除通知。谢谢您的回复,但是您认真考虑过这个问题吗。?这是关于如何删除通知,而不是启动此通知的LED。Thanx用于响应,但您是否认真考虑了这个问题。?这是关于如何删除通知,而不是启动此通知的LED。这是正确的。类似地,如果使用Notification.Builder(或Shreya Shah的评论中指出的NotificationCompat.Builder),请确保在调用.build()之前在生成器上调用setAutoCancel()。这是正确的。类似地,如果使用Notification.Builder(或Shreya Shah的评论中指出的NotificationCompat.Builder),请确保在调用.build()之前先在生成器上调用setAutoCancel()。