Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开最近从Android下载的PDF推送通知_Android_Kotlin_Push Notification - Fatal编程技术网

打开最近从Android下载的PDF推送通知

打开最近从Android下载的PDF推送通知,android,kotlin,push-notification,Android,Kotlin,Push Notification,我会提示用户在设备存储器中下载PDF文件,然后发送一个通知,该通知将使用OnActivityResult()方法中返回的相同Uri打开该文件。但是新的意图似乎无法打开这个Uri,当我点击通知时,什么也没有发生。代码如下: private fun firePushNotification() { val intent: Intent if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent =

我会提示用户在设备存储器中下载PDF文件,然后发送一个通知,该通知将使用OnActivityResult()方法中返回的相同Uri打开该文件。但是新的意图似乎无法打开这个Uri,当我点击通知时,什么也没有发生。代码如下:

private fun firePushNotification() {
    val intent: Intent

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent = Intent(Intent.ACTION_VIEW).apply {
            data = uri // From OnActivityResult()
            type = "application/pdf"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
    } else {
        intent = Intent(Intent.ACTION_VIEW).apply {
            data = uri
            type = "application/pdf"
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
    }

    // Push logic
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
    val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.app_logo)
            .setContentTitle("Download concluído")
            .setContentText("Abrir PDF")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(CHANNEL_ID, "Channel 1",
            NotificationManager.IMPORTANCE_DEFAULT).apply {
            description = ""
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }

    NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
}
我还尝试使用以下代码使用此uri创建文件:

private fun firePushNotification() {
    val intent: Intent

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        val input = context.contentResolver.openInputStream(uri)
        val file = File(context.cacheDir.absolutePath, pdfName)
        val output = FileOutputStream(file)
        copyStream(input!!, output)
        intent = Intent(Intent.ACTION_VIEW).apply {
            data = FileProvider.getUriForFile(context, context.packageName + ".provider", file)
            type = "application/pdf"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
    } else {
        intent = Intent(Intent.ACTION_VIEW).apply {
            data = uri
            type = "application/pdf"
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
    }

    // Push logic ...
}
copystream()
方法:

@Throws(IOException::class)
fun copyStream(inp: InputStream, out: OutputStream) {
    val buffer = ByteArray(1024)
    var read: Int
    while (inp.read(buffer).also { read = it } != -1) {
        out.write(buffer, 0, read)
    }
}

是否有其他方法可以使用用户输入提供的uri打开最近下载的文件?

我看不出您在哪里调用
startActivity()
并传入意图

因此,在解决您的意图后,请尝试:

startActivity(intent)

当我创建通知Compat Builder并将PendingEvent作为contentIntent传递时,应该触发它,对吗?我错了,对不起。是的,你是对的。我真的不明白为什么它不起作用