Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DownloadManager Handle Notification Click Event on Completed Download

I have a problem trying to handle notification click events for downloaded .mp3 files. I already registered the following:

  • DownloadManager.ACTION_NOTIFICATION_CLICKED - I only get the callback from the broadcast receiver when clicking on a notification item that's currently being downloaded (still running).
  • DownloadManager.ACTION_DOWNLOAD_COMPLETE - I get the callback when download completes

The problem is, when I click on a completed download, I get a list of apps that can handle that download .mp3 file (like VLC).

Is there a way to restrict the notification item click so that it's only handled by my app? By the way, my app doesn't even show in the list.

P.S. Would be nice if the solution didn't involve having to create my custom notification handling.

Edit 1: Here's the code to start the download:

private void downloadRequest(int position, String url, String title, String description, String filename) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription(description);
    request.setTitle(title);
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    try {
        request.setDestinationUri(Uri.fromFile(new File(FileUtils.getAudioDirectory(getActivity()) + filename)));
    } catch (IOException e) {
        e.printStackTrace();
    }

    enqueue = mDownloadManager.enqueue(request);

As per matty357's request, here's my broadcast receiver for the notification click:

private BroadcastReceiver receiverNotificationClicked = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String extraId = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
        long[] references = intent.getLongArrayExtra(extraId);
        for(long reference : references) {
            AppLog.d("DOWNLOAD_MANAGER", "reference (clicked): " + reference);
        }
        AppLog.d("DOWNLOAD_MANAGER", "Received click");
    }
};
like image 398
hadez30 Avatar asked Oct 25 '25 21:10

hadez30


1 Answers

The solution for this is to put:

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);

So you won't have a notification when download is finished. And:

context.registerReceiver(onComplete,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

So you will be able to put a notification when download is completed.

BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Log.d(TAG,"onComplete");
        // AND HERE SET YOUR ONW NOTIFICATION WHICH YOU WILL ABLE TO HANDLE
    }
};
like image 57
Damia Fuentes Avatar answered Oct 27 '25 10:10

Damia Fuentes