NativeScript / plugins

@nativescript plugins to help with your developments.
https://docs.nativescript.org/plugins/index.html
Apache License 2.0
190 stars 109 forks source link

[@nativescript/background-http] Error on uploading files #283

Open danielickes opened 2 years ago

danielickes commented 2 years ago

Hello, after updating to newest NativeScript version, we're experiencing issues with file upload. It worked previously with NativeScript 6.

Packages:

"@nativescript/core": "^8.2.2",
"@nativescript/angular": "^13.0.3",
"@nativescript/background-http": "^6.0.0"

Upload config:

const request: Request = {
                url: this._getUrl(fileToUpload.addImageResponse),
                method: "PUT",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "x-ms-blob-content-type": "image/jpeg",
                    "x-ms-blob-type": "BlockBlob"
                },
                description: "Uploading " + fileToUpload.fileName,
                androidAutoClearNotification: true,
                androidAutoDeleteAfterUpload: true
};

const task = this.session.uploadFile(fileToUpload.nativeFile.path, request);

task.on("progress", (e) => this._progressHandler(e as ProgressEventData, fileToUpload));
task.on("error", (e) => this._errorHandler(e as ErrorEventData, fileToUpload));
task.on("responded", this._respondedHandler);
task.on("complete", () => this._completeHandler(fileToUpload));
task.on("cancelled", this._cancelledHandler); // Android only

When executing, we're getting following error:

JS: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'NotificationConfig' of undefined
JS: TypeError: Cannot read property 'NotificationConfig' of undefined
JS:     at setRequestOptions (file: src/webpack:/.../node_modules/@nativescript/background-http/index.android.js:247:0)
JS:     at getBinaryRequest (file: src/webpack:/.../node_modules/@nativescript/background-http/index.android.js:221:0)
JS:     at Function.create (file: src/webpack:/.../node_modules/@nativescript/background-http/index.android.js:152:0)
JS:     at Session.uploadFile (file: src/webpack:/.../node_modules/@nativescript/background-http/index.android.js:139:0)
JS:     at UploadService._uploadToCloud (file: src/webpack:/.../src/app/sl/services/upload/upload.service.ts:251:0)
JS:     at UploadService._checkAndUploadNextFile (file: src/webpack:/.../src/app/sl/services/upload/upload.service.ts:108:0)
JS:     at UploadService._checkAndPrepareNextFile (file: src/webpack:/.../src/app/sl/services/upload/upload.service.ts:97:0)
JS:     at file: src/webpack:/.../src/app/sl/services/upload/upload.service.ts:184:0
JS:     at _ZoneDelegate.in...

Tested on multiple Android devices with android versions from 10 to 12. Not sure if iOS has the same error. Any suggestions?

beloitdavisja commented 2 years ago

Running into this same issue as well. Trying to go from background-http 5.0.2 -> 6.0.0. to get around the issue of targeting Android SDK 31+ (https://github.com/NativeScript/plugins/issues/217). Have been through the docs and everything is setup correctly, including adding the init() in main.ts.

Cateye82 commented 2 years ago

including adding the init() in main.ts.

Saved my week! Thanks! It works in my case with: "@nativescript/core": "~8.2.1", "@nativescript/angular": "^13.0.0", "@nativescript/background-http": "^6.0.0",

I had to create a Notification Channel and attach it to the request:

image

Sorry for the image, code was not working fine. Does that work for you?

srikanthuppena98 commented 1 year ago

including adding the init() in main.ts.

Saved my week! Thanks! It works in my case with: "@nativescript/core": "~8.2.1", "@nativescript/angular": "^13.0.0", "@nativescript/background-http": "^6.0.0",

I had to create a Notification Channel and attach it to the request:

image

Sorry for the image, code was not working fine. Does that work for you?

@Cateye82 can you let me know, whether I can resolve this in nativescript 6x or I have to upgrade to 8x for sure

Cateye82 commented 1 year ago

As far as i remember, the version 6 doesn't need that init() and also not the "if (android.os.Build.Version...." part. So the notificationManager is not needed (skip the first 11 lines of the androidUploadFiles method).

srikanthuppena98 commented 1 year ago

@Cateye82 I need the changes as you mentioned above, with nativescript 6.8.0 & @nativescript/background-http: 6.0.0 version.

Now I'm getting this error "ERROR TypeError: Cannot read property 'NotificationConfig' of undefined"

Can you please let me know how to fix