我需要关闭一个特定的通知,我点击它的按钮。问题是如何知道notification_id以了解哪个关闭。这是我的代码:
intent.setAction("eliminar");
bundle = new Bundle();
bundle.putInt("respuesta", 1);
intent.putExtras(bundle);
pIntent = PendingIntent.getBroadcast(getApplicationContext(), (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
n = new Notification.Builder(getApplicationContext())
.setContentTitle("Recordar")
.setStyle(new Notification.BigTextStyle().bigText(nota.getText()))
.setSmallIcon(R.drawable.ic_icono)
.setContentIntent(pIntent)
.setColor(Color.parseColor("#4CAF50"))
.setOngoing(true) //notificacion persistente
.addAction(R.drawable.ic_borrar, "Quitar nota", pIntent).build();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(cpos, n);
cpos是每个通知的ID
答案 0 :(得分:0)
正如你所说:
cpos是每个通知的ID。
以上陈述是错误的。每个通知都有自己唯一的ID,作为该通知的标识符。
您可以使用notificationManager.cancel(cpos);
清除状态栏中的通知。
您可以创建一个按钮,点击该按钮即可调用:
notificationManager.cancel(cpos);
关闭通知。
答案 1 :(得分:0)
如果您想获得点击的通知ID , 试试this回答。
如果您想显示多个通知,请使用以下命令: -
假设您已创建了2个通知。 确保为每个通知提供不同的预定义 ID。
例如。
通知1:
notificationManager.notify(1, n1);
通知2:
notificationManager.notify(2, n2);
点击按钮:
如果条件匹配condition1
notificationManager.cancel(1);
如果条件匹配condition2
notificationManager.cancel(2);