Search code examples
javaandroidpush-notificationnotificationsandroid-workmanager

WorkManager OneTimeWorkRequest shows only the last notification


my app tracks the movie release dates, notifying the user the day of the release date. It could happen that two or more elements have the same release date and then, at midnight on that day, more notifications must be sent. The problem is that the notifications instead of accumulating replace each other, only showing the latest one.

MainAcitvity:

createWorkRequest(element.getName(), notification_delay_in_seconds);
private void createWorkRequest(String message, long timeDelayInSeconds) {
    OneTimeWorkRequest temp = new OneTimeWorkRequest.Builder(ReminderWorker.class)
            .setInitialDelay(timeDelayInSeconds, TimeUnit.SECONDS)
            .setInputData(new Data.Builder()
                    .putString("title", message)
                    .build()
            )
            .build();

    WorkManager.getInstance(this).enqueue(temp);
}

NotificationHelper:

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

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

public void createNotification(@NotNull String title, @NotNull String message) {
    createNotificationChannel();
    Intent intent = new Intent(context, AnimeActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 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();
    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, 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;
}

}

ReminderWork:

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

@NotNull
public Result doWork() {
    new NotificationHelper(this.context).createNotification(String.valueOf(this.getInputData().getString("title")), "test");
    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;
}

}

I tried also to delay each notification by 5 seconds to test if the problema was show 2 or more notification in the exact same time, but not working anyway, the first one is shown and then it is replaced with the last one, but I would like notifications would pile up instead of replace each other


Solution

  • You are sending the notifications with always the same ID, it means always the same notification, change notification_id for each notification and it will work