Download Manager Progress Not Visible in the Notification Area

Download Manager Progress Not Visible in the Notification Area

Why This Happens in the first place?

The cause of the problem is the Android Download Manager's Notification was disabled in the settings. (It is the new default for my Galaxy S7 - SM930W8 which comes with the new Android N (7.0) Update)

The solution 1

  1. Go to the Settings > App
  2. In the options click 'Show System Apps'
  3. Find 'Download Manager' and open it
  4. Click 'Notifications' under App Settings
  5. Switch on Allow Notifications

After completing the above steps the above piece of code for downloading with notification working just fine.

The Solution 2

The above solution won't work with the general distribution of an application. We can't tell the users to do solution 1.

Adding your own small progress bar to show downloading percentage in your activity will give the user some idea that the file he/she requested is downloading.

Try to avoid notification because if the android download manager is giving the same notification then it is redundant. (default setting of show notification may vary device to device with respect to which ROM they are on)

So here is the code.

Uri uri = Uri.parse("file://" + destination);

url = "http:....."; //Valid File URL

//Set up download manager request

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading " + file_name);
request.setTitle("My Downloader");
request.setDestinationUri(uri); //URI is valid

//Start the download
DownloadManager manager = (DownloadManager) getContext()
.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);

final int UPDATE_PROGRESS = 5020;

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==UPDATE_PROGRESS){
String downloaded = String.format("%.2f MB", (double)((msg.arg1)/1024)/1024);
String total = String.format("%.2f MB", (double) (msg.arg2)/1024)/1024);
String status = downloaded + " / " + total;
pDialog.setTitleText(status);
}
super.handleMessage(msg);
}
};
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
//Post message to UI Thread
Message msg = handler.obtainMessage();
msg.what = UPDATE_PROGRESS;
//msg.obj = statusMessage(cursor);
msg.arg1 = bytes_downloaded;
msg.arg2 = bytes_total;
handler.sendMessage(msg);
cursor.close();
}
}
}).start();

The Solution 3

Adding request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); also helps as @kunal mentioned.

On progress seekbar notification not showing in DownloadManager android

Yes I found solution. The problem is didn't enabled download manager.

Go to the Settings > App
In the options click 'Show System Apps'
Find 'Download Manager' and open it
Click 'Notifications' under App Settings
Switch on Allow Notifications

Reference:

https://stackoverflow.com/a/43841708/1921263

Message from DownloadManager in closed NotificationBar in Android

Notification overview. Method notify. Method cancel.

showing download progress in progress bar using DownloadManager

You have a Logic error, Your Query is out of the while loop where the UI is updated

private void startAppDownload() {


new Thread(new Runnable() {
@Override
public void run() {
boolean isDownloading = true;
int downloadStatus, totalBytesDownloaded, totalBytes;



while (isDownloading) {

DownloadManager.Query downloadQuery = new DownloadManager.Query();
downloadQuery.setFilterById(downloadID);

Cursor cursor = downloadManager.query(downloadQuery);
cursor.moveToFirst();

totalBytesDownloaded = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
);

totalBytes = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
);

downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
isDownloading = false;
break;;
}

final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

runOnUiThread(new Runnable() {
@Override
public void run() {
downloadProgressBar.setProgress(downloadProgress);
}
});
}

cursor.close();
}
}).start();
}

Is there any way to show 'download completed' notification using react-native?

according to https://github.com/cjdell/react-native-fs-test/blob/master/index.common.js

const ret = RNFS.downloadFile({ fromUrl: url, toFile: downloadDest });  
ret.promise.then(res => {
// Show Your Notification
}).catch(err => {});

for notification you can use https://github.com/zo0r/react-native-push-notification, this is an example:

RNFS.downloadFile({
fromUrl,
toFile,
}).promise.then((res) => {
PushNotification.localNotification(details)
});


Related Topics



Leave a reply



Submit