【问题标题】:DownloadManager.Request.setNotificationVisibility not showing Notification in android PDownloadManager.Request.setNotificationVisibility 未在 android P 中显示通知
【发布时间】:2019-06-14 21:58:53
【问题描述】:

我正在下载一个 pdf 文件,下载通知在 marshmallow 中可见,但在 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 中显示下载通知的解决方案?谢谢。

【问题讨论】:

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


    【解决方案1】:

    如果项目面向 Android 9.0(API 级别 28),您需要在清单中添加 FOREGROUND_SERVICE 权限。

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

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

    注意:面向 Android 9.0(API 级别 28)或更高版本并使用 前台服务必须请求 FOREGROUND_SERVICE 权限。 这是正常的权限,所以系统会自动授予 请求的应用程序。

    【讨论】:

    • 下载通知时通知托盘中可见?
    猜你喜欢
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多