quickstart-android icon indicating copy to clipboard operation
quickstart-android copied to clipboard

android firebase file upload in background service stopped when clear app from recents.

Open vidjanidhi opened this issue 6 years ago • 3 comments

Step 1: Are you in the right place?

  • For issues or feature requests related to the code android storage file a Github issue.

Step 2: Describe your environment

  • Android device: _____Mi note5
  • Android OS version: _____oreo
  • Google Play Services version: _____
  • Firebase/Play Services SDK version: _____

Step 3: Describe the problem:

I have used your code for file upload in background service. It works fine when the app is running. but when the app is removed from recent apps or get killed then the uploading and notification stopped and remains as it is.

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NotificationJobService extends JobService implements UploadFileToServer.UploadEventListener {
    private JobStartedListner jobStartedListner;
    ArrayList<File> sourceFileList = new ArrayList<File>();
    UploadFileToServer uploadFileToServer;

    @Override
    public void onPreExecute() {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onProgressUpdated(Integer progress, int totalSize, int currentFilePosition) {

    }

    public interface JobStartedListner {
        public void onJobStarted();
    }

    public NotificationJobService() {
    }

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Log.e("Job Params", jobParameters.toString());
        String[] data = jobParameters.getExtras().getStringArray("data");
        for (int i = 0; i < data.length; i++) {
            if (data[i] != null) {
                File f = new File(data[i]);
                sourceFileList.add(new File(data[i]));
            }
        }
        uploadFiles(this, sourceFileList, this);
        return false;
    }

    public void uploadFiles(UploadFileToServer.UploadEventListener uploadEventListener, ArrayList<File> sourceFileList, Context context) {
        uploadFileToServer = new UploadFileToServer(uploadEventListener, /*new File(imagepath)*/sourceFileList, context);
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        uploadFileToServer.cancel(true);
        return true;
    }
}

vidjanidhi avatar Jan 05 '19 07:01 vidjanidhi

@vidjanidhi thanks for reporting this issue! We should probably update this sample to use WorkManager to reflect modern best practices for async work.

samtstern avatar Jan 10 '19 02:01 samtstern

Hi @samtstern From the initial bit of what I have read workManager is used for synchronous tasks. Can it also do async work?

souragar avatar May 12 '19 06:05 souragar

I hope this gets fixed

obaida-mostarihi avatar Dec 25 '20 02:12 obaida-mostarihi