Search code examples
androidandroid-notificationsandroid-download-manager

Can we have Custom Notification for android.os.DownloadManager?


Currently I am using below code snippet to download a file using DownloadManager:

String servicestring = Context.DOWNLOAD_SERVICE;
                DownloadManager downloadmanager;
                downloadmanager = (DownloadManager) getSystemService(servicestring);
                Uri uri = Uri
                        .parse("some url here");
                DownloadManager.Request request = new Request(uri);

Using this code I am getting below notification.

enter image description here

My question is, can we add some cross button in this notification so that if user click that button it will cancel download?

Expected output:

(user must be able to cancel download when click on this red cross icon)

enter image description here

Please suggest some way if any. Thank you


Solution

  • can we add some cross button in this notification so that if user click that button it will cancel download?

    Not directly. That Notification is not being displayed by your app; you have no control over its behavior.

    The most likely solution is to not use DownloadManager, but instead download the content yourself. Then, you can display whatever you want as a Notification.

    Middle ground would be to use DownloadManager but set up the flags and permissions to allow you to request downloads that do not display a Notification. Then, you can have your own Notification. The downside here is that you may not be able to reproduce all features of the system-supplied Notification (e.g., percent-complete progress indicator).