Как открыть файл PDF из уведомления - PullRequest
0 голосов
/ 19 сентября 2019

Я должен открыть 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 запущено, но файл не открывается, и я не получаю никакой ошибки.

...