2

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

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

4 回答 4

2

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

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

于 2012-12-13T06:02:52.943 回答
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);
于 2012-12-13T05:54:01.680 回答
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);
于 2012-12-13T05:56:26.957 回答
0

它为我工作

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

希望这对你有帮助。

于 2012-12-13T05:59:27.667 回答