Reputation: 386
When I press the "Cancel" button in the notification, the notification still remains in the notification bar, and every time I press "Cancel" the notifications closes but still appears in the top bar, and when I open the notifications is still there, the notification is not being removed properly.
This is the code to create the progress notification, it works fine:
private void createProgressNotification() {
PendingIntent dismissIntent = NotificationActivity.getDismissIntent(NOTIFICATION_PROGRESS, Analyse.this);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("URL Analysis")
.setContentText("Analysis in progress")
.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(false)
.addAction(R.drawable.cancel, "Cancel", dismissIntent)
.setOngoing(true)
.setProgress(0, 0, true);
NotificationManager mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(NOTIFICATION_PROGRESS, mBuilder.build());
}
And this is the custom NotificationActivity class I have to handle the dismissIntent:
public class NotificationActivity extends Activity {
public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
}
public static PendingIntent getDismissIntent(int notificationId, Context context, AsyncCallWS analyze) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(NOTIFICATION_ID, notificationId);
PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return dismissIntent;
}
}
Any idea why this could be happening?
Upvotes: 0
Views: 68
Reputation: 13368
Change false to true in setAutoCancel() of NotificationCompat Builder property
mBuilder.setAutoCancel(true);
so it will be
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("URL Analysis")
.setContentText("Analysis in progress")
.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(true)
.addAction(R.drawable.cancel, "Cancel", dismissIntent)
.setOngoing(true)
.setProgress(0, 0, true);
Upvotes: 1