调用 setProgress(0, 0, true) 不会更新通知 > API 19

Anu*_*bis 5 android android-notifications

更新:

在进行了大量调试后,我找出了通知未更新的原因:setProgress(0, 0, false)下载完成后调用。出于某种原因,当此方法在之前被调用时notify(),即将到来的更新实际上并没有通过。添加 NotificationChannel 不会做任何事情。

我目前的解决方法是调用,setProgress(100, 100, false)以便用户可以告诉下载已完成,随后会得到更新。

原问题:

我有一个自定义文件下载实用程序,可以在下载文件时创建通知。此通知会随着下载的进行而更新。但是,我在 API 级别高于 19 时得到了一些奇怪的结果。下载完成后,NotificationManager 不会更新通知以通知用户这一点。奇怪的是,只要下载进行,通知就会更新。此外,当我激活调试器时,通知会在下载完成时更新。这让我相信这里正在发生某种竞争条件,但我似乎无法真正找出在哪里或如何。

我的 FileDownloader 类:

public static void startGetRequestDownload(final Context context,
                                           String fileUrl,
                                           @Nullable String fileName,
                                           @Nullable Header[] headers,
                                           @Nullable RequestParams requestParams,
                                           final boolean showProgressNotification){

    final int notificationID = 0;
    final NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if(mNotifyManager == null) {
        Log.e(TAG, "Failed to get NotificationManager service");
        return;
    }
    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    String notificationTitle = context.getString(R.string.default_filename);
    if(fileName != null && !fileName.isEmpty()){
        notificationTitle = fileName;
    }
    mBuilder.setContentTitle(notificationTitle)
            .setContentText(context.getString(R.string.download_in_progress))
            .setColor(ContextCompat.getColor(context, R.color.app_color_accent))
            .setSmallIcon(R.drawable.ic_elab);

    String uuid = "Temp_" + System.currentTimeMillis();
    final File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), uuid);
    // Temporarily disable SSL as workaround.
    RestClient.getClient().setSSLSocketFactory(MySSLSocketFactory.getFixedSocketFactory());
    RestClient.getClient().get(context, fileUrl, headers, requestParams, new FileAsyncHttpResponseHandler(tempFile) {

        @Override
        public void onStart() {
            super.onStart();
            if(showProgressNotification){
                mBuilder.setProgress(100, 0, false);
                mNotifyManager.notify(notificationID, mBuilder.build());
            }
        }

        @Override
        public void onProgress(long bytesWritten, long totalSize) {
            super.onProgress(bytesWritten, totalSize);
            if(showProgressNotification){
                int progress = (int) (bytesWritten / totalSize * 100);
                mBuilder.setProgress(100, progress, false);
                mNotifyManager.notify(notificationID, mBuilder.build());
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
            if(showProgressNotification){
                mBuilder.setContentText(context.getString(R.string.download_failed));
                mBuilder.setProgress(0,0,false);
                mNotifyManager.notify(notificationID, mBuilder.build());
            }
            Log.w(TAG, "File download failed", throwable);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, File file) {
            FileMetaData metaData = validateResponse(headers);
            if(metaData != null){
                final File downloadDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
                File newFile = new File(downloadDirectory, metaData.fileName);
                if(file.renameTo(newFile)){
                    Uri fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", newFile);
                    Intent actionViewIntent = new Intent(Intent.ACTION_VIEW);
                    actionViewIntent.setDataAndType(fileUri, metaData.contentType);
                    actionViewIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    if(showProgressNotification){
                        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionViewIntent, PendingIntent.FLAG_ONE_SHOT);
                        mBuilder.setProgress(0,0,false);
                        mBuilder.setContentText(context.getString(R.string.download_completed));
                        mBuilder.setContentIntent(pendingIntent);
                        mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
                        mNotifyManager.notify(notificationID, mBuilder.build());
                    }
                    return;
                }
            }

            //Failover
            if(showProgressNotification){
                mBuilder.setContentText(context.getString(R.string.download_failed));
                mNotifyManager.notify(notificationID, mBuilder.build());
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Anu*_*bis 6

我想出了为什么我的通知没有更新的实际原因。Android 文档提到了以下有关通知更新的内容:

注意:系统对更新通知应用速率限制。如果您过于频繁地发布通知更新,系统可能会丢弃一些通知。

notify()每次下载进行时我都会打电话。显然,下载进度方法被频繁调用,导致多个通知被阻止。但是,我只看到最后一个通知被删除:下载完成通知。

将调用数量限制为notify()inonProgress()修复了我的问题。