ListenableWorker done, but ongoing foreground notification still exists?

Viewed 355

I am trying to use ListenableWorker to perform some background API calls.

During this process I want to display notification with a progress.

I use setForegroundAsync() function as per this documentation google docs

The problem is when my ListenableWorker stops, i can still see my ongong notification and i cant remove it.

This is my refresh fucntion where I change notification parameters:

private void updateRefreshStatus(float objectsProcessed, int totalObjects) {
        if (totalObjects == 0) {
            setProgressAsync(new Data.Builder().putFloat(PROGRESS_CONSTANT_ID, 100.0f).build());
            setForegroundAsync(createForegroundInfo(FloatFormatter.roundFloatTwoDigitsAsString(100f)));
        } else {
            setProgressAsync(new Data.Builder().putFloat(PROGRESS_CONSTANT_ID, (objectsProcessed / (float) totalObjects) * 100f).build());
            setForegroundAsync(createForegroundInfo(FloatFormatter.roundFloatTwoDigitsAsString((objectsProcessed / (float) totalObjects) * 100f)));
        }       
    }

This is how I create my foregroundInfo:

private ForegroundInfo createForegroundInfo(@NonNull String progress) {
        Notification notification = WorkerUtils.prepareProgressNotification("Update",
                "Update progress " + progress + " %",
                getApplicationContext());
        return new ForegroundInfo(NOTIFICATION_PROGRESS_ID,notification);
    }

This is my progress notification code:

static Notification prepareProgressNotification(String title, String message, Context context) {
        // Make a channel if necessary
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            CharSequence name = WorkerConstants.VERBOSE_NOTIFICATION_CHANNEL_NAME;
            String description = WorkerConstants.VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION;
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel =
                    new NotificationChannel(WorkerConstants.CHANNEL_ID, name, importance);
            channel.setDescription(description);

            // Add the channel
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }

        // Create the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, WorkerConstants.CHANNEL_ID)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentText(message)
                .setTicker(title)
                .setOngoing(true)
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVibrate(new long[0]);

        // Show the notification
        return builder.build();
    }

I tried calling cancelAll, cancel(id), but it does nothing in my case.

UPDATE: Removing .setOngoing(true) from builder, does nothing for me its seems like setForegroundAsync() issue?

0 Answers
Related