Esse codego não esta mostrando notificacãoes. public static void startDownload(Context context, String downloadUrl, Uri fileUri, String mimeType, String fileName) {
Log.e("save", "Request to download and save file");
try {
// Construindo a URI do diretório usando a URI da árvore
Uri directoryUri = DocumentsContract.buildDocumentUriUsingTree(fileUri, DocumentsContract.getTreeDocumentId(fileUri));
Log.e("save", "directoryUri: " + directoryUri);
// Criando um novo documento (arquivo) no diretório especificado
Uri newFileUri = DocumentsContract.createDocument(context.getContentResolver(), directoryUri, mimeType, fileName);
Log.e("save", "newFileUri: " + newFileUri);
// Verificando se a URI do novo arquivo foi criada com sucesso
if (newFileUri != null) {
try (OutputStream os = context.getContentResolver().openOutputStream(newFileUri)) {
String cookies = CookieManager.getInstance().getCookie(downloadUrl);
// Conectando ao URL para baixar o arquivo
URL url = new URL(downloadUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "download_channel");
builder.setContentTitle("Download iniciado")
.setContentText("Baixando " + fileName)
.setSmallIcon(R.drawable.lock_icon_green)
.setProgress(100, 0, true);
notificationManager.notify(1, builder.build());
if (cookies != null) {
connection.setRequestProperty("Cookie", cookies);
}
connection.setRequestProperty("User-Agent", appVersion);
try (InputStream is = connection.getInputStream()) {
// Lendo dados do InputStream e escrevendo no OutputStream
int progress = 0;
int notificationId = 1;
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
progress += bytesRead;
//int percentage = (int)((progress / totalSize) * 100);
builder.setProgress(100, progress, false);
// progress += bytesRead;
builder.setContentText(progress + "%");
notificationManager.notify(notificationId, builder.build());
}
builder.setContentText("Download finalizado");
// .setProgress(0, 0, false);
notificationManager.notify(notificationId, builder.build());
Log.e("save", "File download and save successful");
} finally {
connection.disconnect();
}
} catch (Exception e) {
Log.e("FileDownloader", "Failed to download or save file: " + e.getMessage());
e.printStackTrace();
}
} else {
Log.e("FileDownloader", "Failed to create new file URI.");
}
} catch (Exception e) {
Log.e("FileDownloader", "Exception: " + e.getMessage());
e.printStackTrace();
}
}