DownloadManager.Request.setNotificationVisibility在Android P中未显示通知

时间:2019-01-21 13:36:45

标签: android notifications android-notifications android-download-manager download-manager

我正在下载pdf文件,棉花糖中显示了下载通知,但在Android P中不可见。

我的文件可以在Android-M和Android-P中成功下载

我的代码在这里。

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;
    }

谁能给出解决方案以在Android P中显示下载通知?谢谢。

1 个答案:

答案 0 :(得分:0)

如果Project目标是Android 9.0(API级别28),则需要在清单中添加FOREGROUND_SERVICE权限。

将以下代码添加到清单文件中

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

注意:面向Android 9.0(API级别28)或更高版本的应用   前台服务必须请求FOREGROUND_SERVICE权限。   这是普通权限,因此系统会自动将其授予   请求的应用。