Über Notification eine Datei öffnen funktioniert manchmal nicht

  • 0 Antworten
  • Letztes Antwortdatum
AnnaBauer21

AnnaBauer21

Neues Mitglied
4
Hallo ihr Lieben,

ich möchte in einer Notification durch einen Button (Öffnen) eine Datei öffnen. Dafür verwende ich Intent.createChooser.
Doch leider klappt das nicht immer, manchmal öffnet sich nach drücken des Buttons der Chooser und manchmal passiert einfach nichts.

Das Problem ist, dass der Fehler sporadisch auftritt. Ich habe geprüft, ob es an einer bestimmten Dateiendung oder Datei liegt, aber manchmal geht es und manchmal nicht.

Die App ist während die Notification kommt geschlossen.

Ich teste seit Tagen rum und komme nicht weiter, evtl. stimmt auch etwas mit den Flags nicht, ich hoffe ihr könnt mir helfen!

NotificationHelper
Java:
public class NotificationHelper {
    public static void CreateNotification(Context context, String title, String message, String id) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Erstellt einen NotificatonChannel
        CreateNotificationChannel(notificationManager, id);

        // Intent mit Activity erstellen, welches beim Drücken auf die Benachrichtigung geöffnet wird
        Intent openMenuIntent = new Intent(context, MainMenu.class);
        openMenuIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent openMenuPIntent = PendingIntent.getActivity(context, id.hashCode(), openMenuIntent, PendingIntent.FLAG_IMMUTABLE);

        // Action-Button erstellen, der in der Benachrichtigung angezeigt wird. Benötigte Daten für OpenFileReceiver mitgeben
        Intent openFileIntent = new Intent(context, OpenFileReceiver.class);
        openFileIntent.putExtra("FILE_NAME", message);
        openFileIntent.putExtra("ID", id);
        openFileIntent.putExtra("MOVE", false);
        PendingIntent openFilePIntent = PendingIntent.getBroadcast(context, id.hashCode() + 1, openFileIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        NotificationCompat.Action openFileAction = new NotificationCompat.Action.Builder(IconCompat.createWithResource(context, R.mipmap.ic_launcher),"Öffnen", openFilePIntent).build();

        // Action-Button erstellen, der in der Benachrichtigung angezeigt wird
        Intent moveIntent = new Intent(context, OpenFileReceiver.class);
        moveIntent.putExtra("ID", id);
        moveIntent.putExtra("MOVE", true);
        PendingIntent movePIntent = PendingIntent.getBroadcast(context, id.hashCode() + 2, moveIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        NotificationCompat.Action moveAction = new NotificationCompat.Action.Builder(IconCompat.createWithResource(context, R.mipmap.ic_launcher),"Verschieben", movePIntent).build();

        // Benachrichtigung erstellen
        Notification notification = new NotificationCompat.Builder(context, id)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(openMenuPIntent)
                .addAction(openFileAction)
                .addAction(moveAction)
                .setSmallIcon(R.drawable.notification)
                .build();
        notificationManager.notify(id.hashCode(), notification);
    }

    private static void CreateNotificationChannel(NotificationManager notificationManager, String id) {
        NotificationChannel channel = new NotificationChannel(id, id, NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }

    public static void CloseNotification(Context context, String id) {
        NotificationManagerCompat.from(context).cancel(id.hashCode());
    }
}

OpenFilereceiver
Java:
public class OpenFileReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Konstanten neu setzen
        if(Constants.AppFolder == null){
            Constants.SetConstants();
        }

        try {

            // Mitgegebene Daten aus NotificationHelper auslesen
            String id = intent.getStringExtra("ID");

            // Prüft, ob Datei geöffnet oder der Reminder verschoben werden soll
            boolean move = intent.getBooleanExtra("MOVE", false);
            if (!move) {
                String fileName = intent.getStringExtra("FILE_NAME");

                // Öffnet die Datei
                OpenFile(context, id, fileName);

                // Nächste Datei setzen
                XmlHelper.SetNextFile(id);
            }

            // Benachrichtigung schließen
            NotificationHelper.CloseNotification(context, id);

            // Heruntergezogene Statusleiste (Notification Drawer) schließen
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
                Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                context.sendBroadcast(it);
            }
        } catch(Exception e){
            Toast.makeText(context, "FEHLER siehe Log", Toast.LENGTH_LONG).show();
            MyAndroidLib.LogWorker.LogStackTrace(e, "Fehler in OpenFilereceiver");
        }
    }

    public static void OpenFile(Context context, String id, String fileName) {
        // Prüfen ob Datei existiert
        String filePath = XmlHelper.GetReminderPath(id) + File.separator + fileName;
        File dir = new File(filePath);
        if (dir.exists()) {
            Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", dir);

            // Intent zum Öffnen des Dokuments erstellen
            Intent intentShareFile = new Intent(Intent.ACTION_VIEW);
            if (filePath.endsWith(".docx")) {
                intentShareFile.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            } else if (filePath.endsWith(".pdf")) {
                intentShareFile.setDataAndType(uri, "application/pdf");
            } else {
                String[] fileType = filePath.split("\\.");
                Log.d(OpenFileReceiver.class.getName(), "Dateiendung kann nicht geöffnet werden!: " + fileType[fileType.length - 1]);
            }
            intentShareFile.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // Intent zur Auswahl der App erstellen & Dokument öffnen
            Intent chooser = Intent.createChooser(intentShareFile, "Open File Using...");
            chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(chooser);
            Log.d(OpenFileReceiver.class.getName(), "Datei geöffnet!");
        }
    }
}


P.s. Wenn mir jemand sagen kann wie ich in API 30 & 31 die Statusleiste (Notification Drawer) schließen kann, würde mir das zusätzlich eine Freude bereiten :D
 
Zuletzt bearbeitet:

Ähnliche Themen

AnnaBauer21
Antworten
6
Aufrufe
401
AnnaBauer21
AnnaBauer21
S
Antworten
7
Aufrufe
430
Silvasurf
S
D
Antworten
8
Aufrufe
394
jogimuc
J
Zurück
Oben Unten