2017-10-19 4 views
0

다운로드 진행률을 표시하는 알림을 만들고 싶습니다 (지금은 조롱입니다). 사용자가 다운로드를 취소 할 수 있습니다. 알림 빌더를 사용하고 '다운로드 취소'작업을 추가합니다. 조치가 표시되지만 클릭시 PendingIntent를 보내지 않습니다. 나는 contentIntent를 설정함으로써 PendingIntent가 작동하고 있음을 확인했다. 브로드 캐스트 리시버는 콘텐츠 클릭에 대한 메시지를 얻을 수 있지만 동작 클릭에 대한 메시지는 얻을 수 없습니다.알림 작업이 실행되지 않음 PendingIntent

enter image description here

은 DownloadService

val cancelIntent = Intent(applicationContext, NotificationBroadcastReceiver::class.java).apply { 
    action = "xxx.xxx.xxx.CANCEL_DOWNLOAD" 
    putExtra("notification_id", NOT_ID_PROGRESS) 
} 
val pendingIntent = PendingIntent.getBroadcast(applicationContext, 1, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) 

pendingIntent.send() 

val notificationBuilder = NotificationCompat.Builder(applicationContext, "updates").apply { 
    setContentTitle("Title") 
    setContentText("Text") 
    setSmallIcon(R.drawable.ic_logo_full_black) 
    setOnlyAlertOnce(true) 
    setContentIntent(pendingIntent) 
    addAction(NotificationCompat.Action.Builder(R.drawable.ic_delete, "Cancel", pendingIntent).build()) 
} 
startForeground(NOT_ID_PROGRESS, notificationBuilder.build()) 

Thread({ 
    for (i in 0..100) { 
     notificationBuilder.setProgress(100, i, false) 
     notificationManager?.notify(NOT_ID_PROGRESS, notificationBuilder.build()) 
     Thread.sleep(100L) 
    } 
    notificationBuilder.setProgress(0, 0, false) 
    notificationBuilder.setContentText("Download completed") 
    notificationManager?.notify(NOT_ID_COMPLETE, notificationBuilder.build()) 
    onFinish() 
}).start() 

class NotificationBroadcastReceiver : BroadcastReceiver() { 
    override fun onReceive(context: Context?, intent: Intent?) { 
     val notificationId = intent?.getIntExtra("notification_id", 0) ?: 0 
     Log.d(TAG, "NotificationBroadcastReceiver: notificationId = $notificationId") 
     (context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?)?.cancel(notificationId) 
    } 
} 

의 AndroidManifest.xml

NotificationBroadcastReceiver

답변

0

직접 답변을 찾았습니다. 우선, 모든 것이 올바르게 구성되었습니다. 변경해야 할 것은 알림의 업데이트 간격뿐이었습니다. 간격을 약 2000ms으로 설정하면 취소 작업 단추를 선택할 때 클릭 이벤트가 시작되었습니다.

Thread({ 
    for (i in 0..100 step 20) { 
     notificationBuilder.setProgress(100, i, false) 
     notificationManager?.notify(NOT_ID_PROGRESS, notificationBuilder.build()) 
     Thread.sleep(2000L) 
    } 
    notificationBuilder.setProgress(0, 0, false) 
    notificationBuilder.setContentText("Download completed") 
    notificationManager?.notify(NOT_ID_COMPLETE, notificationBuilder.build()) 
    onFinish() 
}).start() 
관련 문제