Android 处理批量时通知内容未更新 notifications/tasks

Android notification content not updating on processing bulk notifications/tasks

当我尝试更新与其任务相关的多个通知的进度和内容时。通知内容未正确更新。

请查看下面的示例代码:

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();
    }
}

我在通知面板上得到以下输出:

You can see final result on above screenshot, only few notifications are updated properly and many notifications are not updated

我认为您正在尝试演示多个通知。 从 android 文档中可以看出,

Caution: Android applies a rate limit when updating a notification. If you post updates to a notification too frequently (many in less than one second), the system might drop some updates.

https://developer.android.com/training/notify-user/build-notification#Updating

我认为这可能是问题所在。这是一个解决方法。当为每个通知启动一个线程时,尝试使用 sleep() 的随机值。附上下面的示例代码

创建一个包含一些长值的数组

public class MainActivity extends AppCompatActivity {
    private NotificationManagerCompat notificationManagerCompat;
    long[] items;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
 items = new long[]{1000,4500,2000,3000,4000,5000,6000,2100,3100,1750,4300,5200,3300,2500,1500,1700,7000,6500,3700,6200};
}
}

在你的初始化函数中

  new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                new Thread() {
                    @Override
                    public void run() {
                        for (int i = 0; i <= 10; i++) {
                            builder.setProgress(10, i, false);
                            builder.setContentText(String.format("Running: %s/%s", i, 10));
                            notificationManagerCompat.notify(id, builder.build());
                            try {
                                Thread.sleep(getRandom(items));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                        builder.setOngoing(false);
                        builder.setSmallIcon(android.R.drawable.stat_sys_download_done);

                        builder.setContentText("Completed");
                        notificationManagerCompat.notify(id, builder.build());
                    }
                }.start();


            }
        });

我在这里使用了处理程序。

也参考这个:Why NotificationManager works so slow during the update progress?