flutter_downloader通知显示错误?

quhf5bfb  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(252)

我正在使用flutter_downloader: ^1.9.1下载文件。它工作正常,文件被下载到下载文件夹中。但问题是文件下载后,通知错误地显示为失败,并且在通知上单击它没有重定向到下载文件夹。您的帮助将保存我的一天。

void _download(String url) async {
    final status = await Permission.storage.request();

    if (status.isGranted) {
      final externalDir = await getExternalStorageDirectory();

      final id = await FlutterDownloader.enqueue(
        fileName: "LRMonoPhase4.mp3",
        url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3',
        savedDir: '/storage/emulated/0/Download',
        showNotification: true,
        openFileFromNotification: true,
      );
    } else {
      print('Permission Denied');
    }
  }

这是提供者

<provider
    android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
    android:authorities="im.mingguang.mingguang_app.flutter_downloader.provider"
    android:exported="false"
    android:grantUriPermissions="true"
    android:requestLegacyExternalStorage="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

最后这是Log

D/DownloadWorker( 4447): DownloadWorker{url=https://www.kozco.com/tech/LRMonoPhase4.mp3,filename=LRMonoPhase4.mp3,savedDir=/data/user/0/com.rsoft.salezrobot/app_flutter,header={},isResume=false,status=ENQUEUED
D/DownloadWorker( 4447): Update notification: {notificationId: 10, title: LRMonoPhase4.mp3, status: RUNNING, progress: 0}
D/DownloadWorker( 4447): Open connection to https://www.kozco.com/tech/LRMonoPhase4.mp3
D/DownloadWorker( 4447): Headers = {}
D/TrafficStats( 4447): tagSocket(172) with statsTag=0xffffffff, statsUid=-1
D/EGL_emulation( 4447): app_time_stats: avg=21.56ms min=5.27ms max=97.80ms count=47
I/trustAllHosts( 4447): checkServerTrusted
D/DownloadWorker( 4447): Content-Type = audio/mpeg
D/DownloadWorker( 4447): Content-Length = 931630
D/DownloadWorker( 4447): Charset = null
D/DownloadWorker( 4447): fileName = LRMonoPhase4.mp3
D/DownloadWorker( 4447): Update notification: {notificationId: 10, title: LRMonoPhase4.mp3, status: RUNNING, progress: 1}
D/EGL_emulation( 4447): app_time_stats: avg=19.26ms min=7.83ms max=39.89ms count=52
D/DownloadWorker( 4447): Update too frequently!!!!, this should be dropped
D/DownloadWorker( 4447): Update too frequently!!!!, this should be dropped

k2arahey

k2arahey1#

根据文档,您的android清单文件中应该缺少一些内容,

<provider
    android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
    android:authorities="${applicationId}.flutter_downloader.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

我不知道它是否链接到通知中显示的“失败”消息,但您应该检查日志以获得更多信息。
最后,必须定义回调。

await FlutterDownloader.registerCallback(callback);

有关回调的更多信息。

ReceivePort _port = ReceivePort();

@override
void initState() {
  super.initState();

  IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
  _port.listen((dynamic data) {
    String id = data[0];
    DownloadTaskStatus status = data[1];
    int progress = data[2];
    setState((){ });
  });

  FlutterDownloader.registerCallback(downloadCallback);
}

@override
void dispose() {
  IsolateNameServer.removePortNameMapping('downloader_send_port');
  super.dispose();
}

@pragma('vm:entry-point')
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
  final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
  send.send([id, status, progress]);
}

注意:在Android上,您只能打开已下载的文件,前提是该文件位于外部存储中,并且设备上至少有一个应用程序可以读取该文件类型。
工作日志示例:

I/trustAllHosts(20572): checkServerTrusted
D/DownloadWorker(20572): Content-Type = application/pdf
D/DownloadWorker(20572): Content-Length = 9263524
D/DownloadWorker(20572): Charset = null
D/DownloadWorker(20572): Content-Disposition = null
D/DownloadWorker(20572): fileName = 6014708.pdf
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Update too frequently!!!!, this should be dropped
D/DownloadWorker(20572): Setting an intent to open the file /storage/emulated/0/Download/6014708.pdf
D/DownloadWorker(20572): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/DownloadWorker(20572): Update notification: {notificationId: 6, title: 6014708.pdf, status: COMPLETE, progress: 100}
D/DownloadWorker(20572): File downloaded
I/WM-WorkerWrapper(20572): Worker result SUCCESS for Work [ id=0b6d1bbe-7c7e-4f48-9783-baed5b3ade51, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]

相关问题