Why does the "download completed" notification disappear on Gingerbread devices?
Asked Answered
P

4

15

I'm using the DownloadManager class to programatically download a file. All works fine but I can't get the download completed notifcation to persist. It disappears immediately once the download has completed. Here's my code:

Request rqtRequest = new Request(Uri.parse(((URI) vewView.getTag()).toString()));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    rqtRequest.setShowRunningNotification(true);  
} else {
    rqtRequest.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).enqueue(rqtRequest);

I've seen some questions around the web relating to this but I couldn't find a solution.

Phares answered 12/11, 2012 at 19:1 Comment(0)
L
16

DownloadManager doesn't support a completion notification on Gingerbread; you have to display it yourself.

Use a BroadcastReceiver to detect when the download finishes and show your own notification:

public class DownloadBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            //Show a notification
        }
    }
}

and register it in your manifest:

<receiver android:name="com.zolmo.twentymm.receivers.DownloadBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
</receiver>

Also, setNotificationVisibility was added in API level 11 (Honeycomb) not ICS. I'm not sure if your use of the ICS constant is deliberate or not, but you can change your code to the following to the use the system notification on Honeycomb as well:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    rqtRequest.setShowRunningNotification(true);  
} else {
    rqtRequest.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
Luddite answered 11/12, 2012 at 16:28 Comment(0)
E
2

You have to create your own download complete notification for Gingerbread.

First, get a reference to the download from the DownloadManager:

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
DownloadManager.Request request = new Request(someUri); 
//...
long downloadReference = downloadManager.enqueue(request);

Then listen for the dowload complete broadcast in your custom BroacastReceiver:

IntentFilter filter = new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE);

BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override public void onReceive( Context context, Intent intent) { 
      long reference = intent.getLongExtra( DownloadManager.EXTRA_DOWNLOAD_ID, -1); 
       if (downloadReference == reference) { 
                // Send your own notification
        } 
     } 
}; 

registerReceiver( receiver, filter);

and send off your own download complete notification.

Eyas answered 12/12, 2012 at 1:56 Comment(1)
The caveat with this approach is that the BroadcastReceiver will be tied to the activity lifecycle. Better to use a manifest registered receiver to raise the notification.Luddite
C
0

Well, which version are you testing on? Setting VISIBILITY_VISIBLE_NOTIFY_COMPLETED should set the notification so that it only displays when the download completes. If the notification is showing up during the download, then I have to assume that you're running on a platform previous to ICS. I'd debug the app. Set breakpoints to see which of your "if" choices is being executed.

Condescension answered 12/11, 2012 at 20:24 Comment(1)
On ICS and newer devices, the notification show up fine — both during the download and persists after the download. On Gingerbread and earlier devices, it shows when downloading but disappears when completed. There's nothing the developer documentation about this. I didn't see a need to put breakpoints because I've tried removing the if statement and running this on a Gingebread emulator. The code gets executed and the notification appears when downloading but that's it.Phares
T
0

Maybe it's a rough (but simple) way: you may prefer just to create new notification after download is finished P.S.:ah, and I'm sorry, it's not actually an answer to question "why", but it still may be helpful for you

Telling answered 11/12, 2012 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.