2017-05-08 110 views
0

我用下面的代码来发送通知:删除通知

NotificationCompat.Builder builder = 
        new NotificationCompat.Builder(getBaseContext()) 
          .setSmallIcon(R.drawable.notification_icon) 
          .setContentTitle("Title") 
          .setContentText("Message"); 
int NOTIFICATION_ID = 12345; 

Intent targetIntent = new Intent(getBaseContext(), MainActivity4.class); 
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.setContentIntent(contentIntent); 
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
nManager.notify(NOTIFICATION_ID, builder.build()); 

代码工作如我所料,除了一个小东西,有一次我去通知栏上,展开它并选择通知,它会一直留在通知栏上,与大多数通知的行为相反,一旦你点击它们,它们就会从那里消失。

猜测它必须是通知的配置中的一些选项,但尽管我搜索我无法找到它。

希望你能帮上忙。

回答

1

你在找吗?

.setAutoCancel(true)

所以你必须

NotificationCompat.Builder builder = 
        new NotificationCompat.Builder(getBaseContext()) 
          .setSmallIcon(R.drawable.notification_icon) 
.setAutoCancel(true) 
          .setContentTitle("Title") 
          .setContentText("Message"); 
+0

是的,正是这样。谢谢。 – user2638180