Android 如何使用通知操作取消Work Manager中的firebase存储上载?
我正在使用工作管理器将一些文件上载到Firebase存储。此外,我还通过通知显示上传进度。我想在通知中单击“取消”操作时取消上载。我正在使用广播接收器进行通知操作。但我怎么知道在work manager中单击“取消”时会发生什么呢 UploadWorker.ktAndroid 如何使用通知操作取消Work Manager中的firebase存储上载?,android,broadcastreceiver,firebase-storage,android-notifications,android-workmanager,Android,Broadcastreceiver,Firebase Storage,Android Notifications,Android Workmanager,我正在使用工作管理器将一些文件上载到Firebase存储。此外,我还通过通知显示上传进度。我想在通知中单击“取消”操作时取消上载。我正在使用广播接收器进行通知操作。但我怎么知道在work manager中单击“取消”时会发生什么呢 UploadWorker.kt class UploadWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
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, 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,
0, 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, 0, 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____ ")
}
}