When I try to update progress and content of multiple notifications related to their tasks. Notification content is not updating properly.
Please checkout below example code:
package dev.na;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManagerCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
if (Build.VERSION.SDK_INT >= 26) {
createNotificationChannel();
}
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 1; i <= 20; i++) {
init(i, "My Simple Task " + i);
}
}
});
}
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel("bulk_tasks", "Bulk Tasks", NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null, null);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManagerCompat.createNotificationChannel(channel);
}
private void init(final int id, String title) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "bulk_tasks");
builder.setShowWhen(false);
builder.setOnlyAlertOnce(true);
builder.setSubText("Processing Task");
builder.setContentTitle(title);
builder.setGroup("TASK_" + id);
(new Thread() {
@Override
public void run() {
int i = 10;
while (i > 0) {
builder.setOngoing(true);
builder.setSmallIcon(android.R.drawable.stat_sys_download);
builder.setProgress(10, 10 - i, false);
builder.setContentText(String.format("Running: %s/%s", 10 - i, 10));
notificationManagerCompat.notify(id, builder.build());
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
i--;
}
builder.setOngoing(false);
builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
builder.setProgress(0, 0, false);
builder.setContentText("Completed");
notificationManagerCompat.notify(id, builder.build());
}
}).start();
}
}
I'm getting this below output on notification panel:
You can see final result on above screenshot, only few notifications are updated properly and many notifications are not updated
from Android notification content not updating on processing bulk notifications/tasks
No comments:
Post a Comment