This commit is contained in:
X1nto 2020-06-30 12:32:51 +04:00
parent 2dacfb0616
commit b569ec6bd5
2 changed files with 9 additions and 9 deletions

View File

@ -9,9 +9,8 @@ import com.downloader.PRDownloader
class DownloadBroadcastReceiver: BroadcastReceiver() { class DownloadBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
val tag: Int? = intent?.getIntExtra("dwnldId", 0) val tag: Int? = intent?.getIntExtra("tag", 0)
PRDownloader.cancel(tag) PRDownloader.cancel(tag)
Log.d("VMNotification", "Canceled ${intent?.getIntExtra("dwnldId", 0)} download") Log.d("VMNotification", "Canceled $tag download")
} }
} }

View File

@ -17,29 +17,30 @@ object NotificationHelper {
val notifChannel = NotificationChannel( val notifChannel = NotificationChannel(
"69420", "69420",
context.getString(R.string.notif_channel_name), context.getString(R.string.notif_channel_name),
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_HIGH
) )
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notifChannel) notificationManager.createNotificationChannel(notifChannel)
} }
} }
fun displayDownloadNotif(channel: Int, progress:Int, filename: String, downId: Int, context: Context) { fun displayDownloadNotif(channel: Int, progress:Int, filename: String, tag: Int, context: Context) {
val notifBuilder = val notifBuilder =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(context, channel.toString()).setChannelId("69420") Notification.Builder(context, channel.toString()).setChannelId("69420")
else else
Notification.Builder(context).setPriority(Notification.PRIORITY_LOW) Notification.Builder(context).setPriority(Notification.PRIORITY_HIGH)
val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java)
cancelDownload.putExtra("tag", tag)
val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java).apply {
putExtra("dwnldId", downId)
}
val cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelDownload, PendingIntent.FLAG_UPDATE_CURRENT) val cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelDownload, PendingIntent.FLAG_UPDATE_CURRENT)
notifBuilder.apply { notifBuilder.apply {
setContentTitle(context.getString(R.string.app_name)) setContentTitle(context.getString(R.string.app_name))
setContentText(context.getString(R.string.downloading_file, filename)) setContentText(context.getString(R.string.downloading_file, filename))
setSmallIcon(R.drawable.ic_stat_name) setSmallIcon(R.drawable.ic_stat_name)
setOnlyAlertOnce(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
addAction(Notification.Action.Builder(null, context.getString(R.string.cancel), cancelPendingIntent).build()) addAction(Notification.Action.Builder(null, context.getString(R.string.cancel), cancelPendingIntent).build())
else else