Upload file with Capacitor and background mode

#1

Hi Everyone,

I am really digging Capacitor!! I have one snag on IOS. Hoping to get some help.

I would like for a user to snap a photo and then close the app right away. The photo gets uploaded to our servers. If they close the app right away and the upload has not completed it will complete in background mode.

I am using ionic 4 and Angular 7.2. I am using HttpClient to do the upload.

Below you can use my code where I am wrapping the upload with a BackgroundTask.

My issue is when the app closes and goes into background mode. The upload fails. It seems networking just dies in background mode.

I have to even just start the upload once in background mode. With no luck.

Once the app comes back into focus whatever networking I tried in background mode starts up.

Any ideas on how to keep the networking going in background mode?

Thanks.
~Spicer

		formData.append('file', imgBlob, this.createFileName(data.type));
		formData.append('note', data.note);

		// In this function We might finish the upload
		let taskId = BackgroundTask.beforeExit(async () => {
			// Log
			console.log("Starting upload.");
		
			// Post file to server
			this.uploadService.create(formData).subscribe(
				res => {
					// Reload  data.
					this.loadData();
		
					// Log
					console.log("Done uploading.");
		
					console.log(res);
					BackgroundTask.finish({ taskId });
				},
		
				error => {
					// Show error in an alert
					console.log(error);
		
					BackgroundTask.finish({ taskId });
				}
			);
		});

Here is what the upload service looks like.

	//
	// Constructor
	//
	constructor(private http: HttpClient) { }

	//
	// Create new upload
	//
	create(formData: FormData): Observable<Upload> {
		let accountId = localStorage.getItem('account_id');
		return this.http.post<Upload>(`${environment.app_server}/api/v3/${accountId}/upload`, formData)
			.pipe(retry(10))
			.pipe(map(res => new Upload().deserialize(res)));
	}