Now in android, I put this code in one action to show a notification when a button is clicked.
static int notificationCount = 0;
then
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);
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);
Notification noti = nBuilder.build();
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(notificationCount++,noti);
}
}
);
From the notification manager. Any notification that I clicked on it will redirect me to another activity (NotificationActivity).
Now I put this code to clear the notification, but it only clears the notification with identifier 0, so how can I delete the current pressed notification
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);
}
I need to clear the notification by its identifier, if possible.
source
share