Использование DownloadManager в FireBaseMessagingService - PullRequest
0 голосов
/ 27 апреля 2020

Я хочу скачать pfd-файл, когда через FCM отправляется уведомление c. Я написал сервис, который расширяет FirebaseMessagingService и в событии onMessageReceived после отображения уведомления, что я хочу скачать файл. Этот процесс работает, только когда приложение активно, когда приложение находится в фоновом режиме, оно не работает.

Это из-за ограничения или вам нужно обойти это, чтобы достичь этого? Основа c Идея состоит в том, чтобы отправить уведомление pu sh с URL-адресом в нем, а доступ к URL-адресу в onMessageReceived приведет к загрузке файла с веб-сервера. Даже если приложение остановлено / в фоновом режиме.

Вот код.

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();

        downloadPdf("www.dummmy.file/file.pdf");
    }
public void downloadPdf(String url) {
        DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
        request.setTitle("My File");
        request.setDescription("Downloading");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setVisibleInDownloadsUi(false);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"downloadfileName");

        downloadmanager.enqueue(request);
    }
...