I am downloading a pdf file and notification for download is visible in marshmallow but it is not Visible in Android P.

My Files downloads successfully in both Android-M and Android-P

My code is here.

public static long DownloadData (Uri uri, Context context,String dir,String fileName,String title,String discription) {
        if(title==null){
            title="";
        }
        if(discription==null){
            discription="";
        }
        long downloadReference;
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        // Create request for android download manager
        downloadManager = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        request.setVisibleInDownloadsUi (true);
        request.setNotificationVisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.addRequestHeader("Authorization", "Bearer " + sp.getString(SharedPreferenceKeys.PREF_AUTH_TOKEN, "Woof"));

        //Setting title of request
        request.setTitle(title);
        //Setting description of request
        request.setDescription(discription);
            request.setDestinationInExternalPublicDir(
                    dir,fileName);
               //Enqueue download and save into referenceId
        downloadReference = downloadManager.enqueue(request);

        return downloadReference;
    }

Can anyone give solution to show Download Notification in Android P as well? thanks.

Answer

If Project target Android 9.0 (API level 28), You Need to add FOREGROUND_SERVICE permission in manifest.

Add Below code to your Manifest file

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Note: Apps that target Android 9.0 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app.

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions