如何从通知中打开pdf文件

时间:2019-09-19 11:23:23

标签: android pdf android-notifications android-pendingintent

点击通知后,我必须打开pdf文件。我尝试这样做:

public void addNotification(File file) {
    createNotificationChannel();


    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
    intent.setDataAndType(uri, "application/pdf");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "0");
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setContentTitle(file.getName() + " creato");
    builder.setContentText("Il tuo pdf è stato salvato nella cartella download");
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setAutoCancel(true);
    builder.setContentIntent(pendingIntent);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(0, builder.build());
}

public void createNotificationChannel(){
    CharSequence name = "Personal";
    String description = "";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel("0", name, importance);
        notificationChannel.setDescription(description);
        NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }

}

它似乎可以工作,因为pdf阅读器应用程序已启动,但文件未打开,并且我没有收到任何错误。

0 个答案:

没有答案