Android 安卓通知不';t单击通知后消失

Android 安卓通知不';t单击通知后消失,android,notifications,Android,Notifications,如果我想在通知栏中显示通知的某些问题。虽然我将通知标志设置为notification.DEFAULT\u LIGHTS¬ification.flag\u AUTO\u CANCEL但单击后通知不会消失。知道我做错了什么吗 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawa

如果我想在通知栏中显示通知的某些问题。虽然我将通知标志设置为
notification.DEFAULT\u LIGHTS¬ification.flag\u AUTO\u CANCEL
但单击后通知不会消失。知道我做错了什么吗

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.icon;
    CharSequence tickerText = "Ticker Text";
    long time = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, time);
    notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 

    Context context = getApplicationContext();
    CharSequence contentTitle = "Title";
    CharSequence contentText = "Text";
    Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(1,notification);
从文件中:

位按位或入应设置的标志字段 用户单击通知时是否应取消通知


通过
NotificationBuilder
生成
通知时,可以使用
NotificationBuilder.setAutoCancel(true)

2016状态:您可以使用
mBuilder.setAutoCancel(true)


来源:

使用标志通知。标志自动取消

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
要启动应用程序,请执行以下操作:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(context, App.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

我的答案是使用
mBuilder.setContinuous(false)

删除通知 通知保持可见,直到发生以下情况之一:

  • 用户将取消通知
  • 用户单击通知,您在创建通知时调用了setAutoCancel()
  • 为特定通知ID调用cancel()。此方法还删除正在进行的通知
  • 调用cancelAll(),它将删除以前发出的所有通知
  • 如果在使用setTimeoutAfter()创建通知时设置了超时,则系统将在指定的持续时间过后取消通知。如果需要,可以在指定的超时持续时间过去之前取消通知
  • 有关更多详细信息,请参阅:

    这不是正确答案<代码>通知。默认指示灯是
    通知的一部分。默认指示灯是
    通知类的一部分,而不是
    通知标志类的一部分。请参阅我的答案以了解相应的setters.notification.flags=notification.DEFAULT_LIGHTS | notification.FLAG_AUTO_CANCEL;此方法正在工作。谢谢synic。此答案中的代码导致通知声音被播放多次。看看其他的答案。我已经看过android文档了。我不太明白什么时候该用旗子。为什么不仅仅是notification.defaults=notification.DEFAULT\u灯光足以显示灯光。因为振动和声音在没有标志的情况下工作。我使用NotificationBuilder,NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this).setMallicon(android.R.drawable.ic_popup_sync).setContentTitle(“new Tweet”).setContentText(“有”+count+“tweets”);mBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);那么,使用Notification
    mNotificationManager.notify(1,Notification);
    和使用NotificationBuilder
    mNotificationManager.notify(1,mBuilder.build())创建通知有什么区别呢
    ?谢谢。这个答案应该被接受,它更符合当前的android设计理论。这个答案是正确的。被接受的答案是有效的,但并不总是有效的。当GCM(或您正在使用的任何东西)上有堆叠的通知时会出现问题。ping通知服务器后,它返回大量通知,有时它只是循环通知外观。
    notificationBuilder.setAutoCancel(true);
    对我不起作用。我是否应该将其放在挂起的意图之前?
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
    
    // Cancel the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    Intent intent = new Intent(context, App.class);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);