D
D
Dmtm2019-04-16 14:08:45
Android
Dmtm, 2019-04-16 14:08:45

Why does clicking on a button in a notification not work in A4.4, but works in A7.0?

4.4 emulator, 7.0 - phone

service
if (intent.action == ACTION_STOP_DOWNLOADING) {
            Log.d(TAG, "onStartCommand, ACTION_STOP_DOWNLOADING")
            mapLoaderBinder.stop()
        } else {
            Log.d(TAG, "onStartCommand, start foreground")
            val contentTitle = intent.getStringExtra(KEY_TITLE)
            val contentText = intent.getStringExtra(KEY_TEXT)

            val notificationIntent = Intent(this, MainActivity::class.java)
            val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

            val notification = AppNotificationCompat.Builder(this, NotificationUtils.DOWNLOADING_MAP_CHANNEL_ID)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setSmallIcon(R.drawable.ic_file_download)
                .setOngoing(true)
                .setContentIntent(pendingIntent)

            startForeground(1612, notification.build())
        }
        return START_STICKY
    }

I see in the onStartCommand ACTION_STOP_DOWNLOADING logs only after the background task has completed, and immediately all the clicks are
visually pressed, the Stop button blinks when pressed
notification
fun showDownloadProgress(fileName: String, progressValue: Int) {
            val notificationBuilder = NotificationCompat.Builder(App.instance, DOWNLOADING_MAP_CHANNEL_ID)

            val contentIntent = Intent(App.instance, MainActivity::class.java)
            val contentPendingIntent = PendingIntent.getActivity(App.instance, 0, contentIntent, 0)

            val buttonCloseIntent = Intent(App.instance, MapLoadingService::class.java)
            buttonCloseIntent.action = MapLoadingService.ACTION_STOP_DOWNLOADING
            val buttonClosePendingIntent = PendingIntent.getService(App.instance, 0, buttonCloseIntent, PendingIntent.FLAG_UPDATE_CURRENT)
            val stopButtonAction = NotificationCompat.Action.Builder(R.drawable.ic_stop, "Stop", buttonClosePendingIntent).build()

            notificationBuilder
                .setSmallIcon(R.drawable.ic_file_download)
                .setPriority(Notification.PRIORITY_LOW)
                .setContentTitle("Loading map file")
                .setContentText(fileName)
                .setContentInfo(progressValue.toString())
                .setProgress(100, progressValue, false)
                //.setContentIntent(contentPendingIntent)
                .addAction(stopButtonAction)

            val notificationManager = App.instance.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, notificationBuilder.build())
        }
if you add .setContentIntent(contentPendingIntent) then switching to activity occurs immediately, and button clicks do not appear in the logs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2019-04-17
@Dmtm

Everything turned out to be working, but, in my service, an observer is started on LiveData
App.instance.data.ftpDownloader.observeForever(ftpProgressObserver)
and when the files are downloaded, it is no longer needed and is deleted directly in it

private val ftpProgressObserver = Observer<CoroutineResultData<LoadingProgressFileData>> {
....
if (files.size > 0) {
                        val file = files.removeAt(0)
                        val client = App.instance.data.ftpClient.value
                        if (client?.data != null) {                     
                            App.instance.data.ftpDownloader.downloadFile(client.data, file)
                        }
                    } else {
                        Log.d(TAG, "before : NotificationUtils.hideDownloadProgress()")
                        NotificationUtils.hideDownloadProgress()                        
                        removeLoadingProgressObserver()
                    }
....
}

...
 fun hideDownloadProgress() {
            Log.d(TAG, "hideDownloadProgress() called")
....
}

and it turns out that the method that launches the notification, which should hide the progress, does not have time to start
(it is interesting that if you set more output to the log, then it succeeds)
though it is not clear why I see "before : NotificationUtils.hideDownloadProgress()" but I don't see "hideDownloadProgress() called" which is the first line in the method
that removeLoadingProgressObserver() is called before NotificationUtils.hideDownloadProgress()?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question