You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WorkManager OneTimeWorkRequest仅显示最后一条通知问题求助

解决Android通知被覆盖无法堆叠的问题

问题描述

应用用于追踪电影上映日期,上映当天推送通知。当同一天有多部电影上映时,多条通知会互相覆盖,仅能看到最后一条,即使给每条通知设置5秒延迟也无法解决。

问题根源

  • 通知ID重复:NotificationHelper中NOTIFICATION_ID被固定为1,每次调用notify()时使用同一个ID,新通知会直接覆盖旧通知。
  • PendingIntent请求码重复:PendingIntent.getActivity()的requestCode固定为0,可能导致不同通知的PendingIntent被复用,影响通知的独立性。

解决方案

1. 为每个通知生成唯一ID

可以通过以下方式生成唯一ID:

  • 使用电影名称的哈希值(确保名称唯一的情况下)
  • 使用UUID生成唯一值
  • 使用递增的整数(需持久化记录当前值)

2. 为PendingIntent设置唯一请求码

确保每个通知的PendingIntent使用不同的requestCode,避免复用。

修改后的代码示例

调整NotificationHelper类

public final class NotificationHelper {
    private final String CHANNEL_ID = "reminder_channel_id";
    private final Context context;

    public NotificationHelper(@NotNull Context context) {
        this.context = context;
    }

    // 修改方法,接收唯一通知ID参数
    public void createNotification(@NotNull String title, @NotNull String message, int uniqueNotificationId) {
        createNotificationChannel();
        Intent intent = new Intent(context, AnimeActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        // 使用uniqueNotificationId作为PendingIntent的requestCode
        PendingIntent pendingIntent = PendingIntent.getActivity(context, uniqueNotificationId, intent, PendingIntent.FLAG_IMMUTABLE);
        Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.xxx)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
        // 使用传入的唯一ID发送通知
        NotificationManagerCompat.from(context).notify(uniqueNotificationId, notification);
    }

    private final void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(this.CHANNEL_ID, this.CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Reminder Channel Description");
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @NotNull
    public final Context getContext() {
        return this.context;
    }
}

调整ReminderWorker类

public final class ReminderWorker extends Worker {
    @NotNull
    private final Context context;
    @NotNull
    private final WorkerParameters params;

    @NotNull
    public Result doWork() {
        String title = getInputData().getString("title");
        // 使用标题的哈希值作为唯一通知ID(也可以用UUID.randomUUID().hashCode())
        int uniqueId = title != null ? title.hashCode() : UUID.randomUUID().hashCode();
        new NotificationHelper(this.context).createNotification(title, "test", uniqueId);
        return Result.success();
    }

    public ReminderWorker(@NotNull Context context, @NotNull WorkerParameters params) {
        super(context, params);
        this.context = context;
        this.params = params;
    }

    @NotNull
    public final Context getContext() {
        return this.context;
    }

    @NotNull
    public final WorkerParameters getParams() {
        return this.params;
    }
}

可选优化:通知分组(如需折叠显示)

如果希望同类型通知可以折叠显示,可以为通知设置相同的groupId:

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        // ...其他设置
        .setGroup("MOVIE_REMINDER_GROUP")
        .build();

同时创建一个组通知(可选):

Notification summaryNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.xxx)
        .setContentTitle("多部电影今日上映")
        .setContentText("点击查看详情")
        .setStyle(new NotificationCompat.InboxStyle()
                .addLine("电影A")
                .addLine("电影B")
                .setBigContentTitle("多部电影今日上映"))
        .setGroup("MOVIE_REMINDER_GROUP")
        .setGroupSummary(true)
        .build();
NotificationManagerCompat.from(context).notify(0, summaryNotification);

内容的提问来源于stack exchange,提问作者C-Gian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 08:35:18