如何解决如何使用通知操作在Work Manager中取消Firebase存储上载?
我正在使用工作管理器将一些文件上传到Firebase存储。另外,我正在显示通过通知上传的进度。我想在通知中单击“取消”操作时取消上传。我正在使用广播接收器进行通知操作。但是,当单击“取消”时,我怎么在工作管理器中知道?
UploadWorker.kt
class UploadWorker(appContext: Context,workerParams: WorkerParameters)
: Worker(appContext,workerParams) {
//...
override fun doWork(): Result {
//...
val uploadTask = storageRef.putFile(fileUri)
notificationBuilder = notificationForUpload(maxUploadSize)
uploadTask..addOnCanceledListener {
notificationBuilder
.setContentTitle("Upload canceled")
.setOngoing(false)
.setProgress(0,false)
notificationManager.notify(UPLOAD_NOTIFICATION_ID,notificationBuilder.build())
}
//...
}
//...
private fun notificationForUpload(progressMax: Int): NotificationCompat.Builder{
val broadcastIntent = Intent(applicationContext,NotificationReceiver::class.java)
val actionIntent = PendingIntent.getBroadcast(
applicationContext,broadcastIntent,PendingIntent.FLAG_UPDATE_CURRENT
)
return NotificationCompat.Builder(applicationContext,CHANNEL_UPLOAD_NOTE)
.setSmallIcon(R.drawable.ic_document)
.setContentTitle("Uploading ${inputData.getString("title") ?: "NoTitle"}")
.setContentText("Total Size ${fileSizeToReadableSize(totalAttachmentsSize.toString())}")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setProgress(progressMax,false)
.addAction(R.mipmap.ic_launcher,"Cancel",actionIntent)
}
NotificationReceiver.kt(广播接收器)
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?,intent: Intent?) {
Log.d(TAG,"onReceive: called____ ")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。