ListenableWorker does not remove notification icon

╄→гoц情女王★ 提交于 2020-07-18 22:35:32

问题


I am using ListenableWorker to perform background tasks. Also I want OS to be aware of my service importance, so I call

 setForegroundAsync(new ForegroundInfo(WorkerConstants.NOTIFICATION_FOREGROUND_ID,
 builder.build()));

As suggested in the google documentation.

But when my service stopped or cancelled, i can still see foreground service notification and i cannot remove it.

Furthermore, I added cancel button to this notification, but it does nothing, I cannot close it:

PendingIntent intent = WorkManager.getInstance(getApplicationContext())
                            .createCancelPendingIntent(getId());
                    builder.addAction(R.drawable.ic_redesign_exit,"Stop",intent);

Any suggestions?


回答1:


I filed an issue to google tracker with this. And found out that was totally worng.

Official response as follows:

Looking at your sample app, you are introducing a race between completion of your ListenableFuture returned by CallbackToFutureAdaptor, and the main looper.

Even without the use of REPLACE one of your Notification updates is posted after your Worker is marked as successful.

However, you can actually avoid the race condition by waiting for the call to setForegroundAsync() to be completed (because that also returns a ListenableFuture). Once you do that - you never have a notification that shows up after your Worker is done.

I will also go ahead and file an improvement on how we can automatically do this even when developers get this wrong.

try {
    // This ensures that you waiting for the Notification update to be done.
    setForegroundAsync(createForegroundInfo(0)).get();
} catch (Throwable throwable) {
    // Handle this exception gracefully
    Log.e("FgLW", "Something bad happened", throwable);
}



回答2:


Better forget the setForegroundAsync and go for the traditional way as explained here: https://developer.android.com/training/notify-user/build-notification

Here is what I used:

    Context context = getApplicationContext();
    String title = context.getString(R.string.str_synchronisation);
    String cancel = context.getString(R.string.str_cancel_synchronisation);
    // This PendingIntent can be used to cancel the worker
    PendingIntent intent = WorkManager.getInstance(context)
            .createCancelPendingIntent(getId());
    //notificationManager.cancel(notification_ID);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notif_ch_id)
            .setSmallIcon(R.drawable.ic_sync_black_24dp)
            .setContentTitle(title)
            .setContentText("Use this Content")
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
//                // Add the cancel action to the notification which can
//                // be used to cancel the worker
            .addAction(android.R.drawable.ic_delete, cancel, intent);
    notificationManager.notify(notification_ID, builder.build());

don't forget to keep an instance of the notification manager:

notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

Update your notification with:

builder.setContentText(context.getString(R.string.str_veuiller_patienter)
                + ", fichier " + String.valueOf(i + 1) + totalfiles);
notificationManager.notify(notification_ID, builder.build());

cancel your notification with:

notificationManager.cancel(notification_ID);


来源:https://stackoverflow.com/questions/60541280/listenableworker-does-not-remove-notification-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!