В Android, как я могу узнать текущий идентификатор уведомления, чтобы очистить уведомление
Теперь в Android я поместил этот код в одном упражнении, чтобы показать уведомление при нажатии кнопки.
static int notificationCount = 0;
затем
btnNotification.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
// Construct the notification
Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
nBuilder.setContentTitle("You Have a notification!");
nBuilder.setContentText("See Your Notification");
nBuilder.setSmallIcon(android.R.drawable.btn_star);
nBuilder.setContentIntent(pIntent);
nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16
// Build the notification
Notification noti = nBuilder.build(); // required api 16
//Send it to manager
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(notificationCount++,noti);
}
}
);
Из диспетчера уведомлений, любое уведомление, что я нажал на него, оно перенаправит меня к другой деятельности (NotificationActivity)
Теперь я поместил этот код, чтобы очистить уведомление, но он очищает только уведомление с идентификатором 0, так как я могу очистить текущее нажатое уведомление
public class NotificationActivitty extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(0);
// manager.cancelAll(); // Cancel all notifications for this app. from manager
}
Мне нужно очистить уведомление по его идентификатору, если это возможно.