The next NotificationUtil helps me to show the notification with PDF file, but when I click on notification and select the PDF viewer, it shows me the next message: ‘You can not open file’.
object NotificationUtil {
private const val CHANNEL_ID = "download_notification_channel"
private var NOTIFICATION_ID = 123
fun showNotification(context: Context, file: File, typeFile: String) {
val channel = NotificationChannel(
CHANNEL_ID,
"Descargas",
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
// Get URI
val fileUri = Uri.fromFile(file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(fileUri, typeFile)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
// Build notification
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(file.name)
.setContentText("Se completó la descarga")
.setSmallIcon(R.drawable.download)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
// Show notification
notificationManager.notify(NOTIFICATION_ID++, builder.build())
}
}
I would like to open PDF file when I click on the notification.
New contributor
Mauricio Carmen Liñan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.