ListenableWorker不会删除通知图标 [英] ListenableWorker does not remove notification icon
问题描述
我正在使用ListenableWorker执行后台任务. 另外,我希望操作系统意识到我的服务重要性,所以我打电话给
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()));
如google文档中所建议.
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);
有什么建议吗?
推荐答案
我为此向Google跟踪器提出了一个问题.并发现那完全是累了.
I filed an issue to google tracker with this. And found out that was totally worng.
官方回应如下:
看看您的示例应用程序,您正在介绍 完成的您的ListenableFuture返回 CallbackToFutureAdaptor和主循环程序.
Looking at your sample app, you are introducing a race between completion of your ListenableFuture returned by CallbackToFutureAdaptor, and the main looper.
即使不使用REPLACE,您的一个通知更新也是: 在您的工作人员标记为成功之后发布.
Even without the use of REPLACE one of your Notification updates is posted after your Worker is marked as successful.
但是,您实际上可以通过等待 调用setForegroundAsync()以完成操作(因为这也会 返回一个ListenableFuture).一旦您这样做-您将永远不会 在您的工作人员完成后显示的通知.
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);
}
这篇关于ListenableWorker不会删除通知图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!