I implemented push notification with loading.

And in theory, as soon as the download ends, a new notification without a download is received, which replaces the old one (that is, in the end we always see only one notification on the screen)

Here is the code

 public void Progress_notif(Context context) { final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_autorenew_white_24dp) .setContentTitle("My notification") .setContentText("Hello World!") .setAutoCancel(true); Intent resultIntent = new Intent(context, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); new Thread( new Runnable() { @Override public void run() { int incr; for (incr = 0; incr <= 100; incr += 10) { mBuilder.setProgress(100, incr, false); mNotificationManager.notify(0, mBuilder.build()); try { Thread.sleep(500); } catch (InterruptedException e) { Log.d("TAG", "sleep failure"); } } mBuilder.setContentText("Download complete").setProgress(0, 0, false); mNotificationManager.notify(10, mBuilder.build()); } } ).start(); } 

Everything works fine, only when the download ends, the notification not replaced, but the new notification is simply created and appears below ...

How to make the old notification disappear and be replaced by a new one?

What did I miss?

    1 answer 1

    You have the first notify parameter different:

     mNotificationManager.notify(0, mBuilder.build()); ... mNotificationManager.notify(10, mBuilder.build()); 

    Those. These are different notifications. RTFM notify ()

    id int: An identifier for your application.