androidvue.jsnativescriptnativescript-vuejobservice

How to make a JobService in nativescript-vue


I searched in the google for several hours, but I got only 3 pages with this questions and non of them helped me.

Im on tns: 5.3.0

TestJobService.js

android.app.job.JobService.extend("org.nativescipt.ITPalert.TestJobService", {
 onStartJob() {
  console.log("Job execution ...");
  // here you can do whatever you want
  //this.jobFinished(params, true); //this ends the job if successful, if not `return false;`
  return true;
 },

 onStopJob() {
  console.log("Stopping job ...");
  return true; //returning true makes the task reschedule
 },
});

Home.vue

startBackground() {
    const context = utils.ad.getApplicationContext();
    const component = new android.content.ComponentName(context, org.nativescipt.ITPalert.TestJobService.class);
    const builder = new android.app.job.JobInfo.Builder(1, component);
    builder.setRequiredNetworkType(android.app.job.JobInfo.NETWORK_TYPE_ANY);
    builder.setMinimumLatency(1000 * 60)
    builder.setOverrideDeadline(1000 * 100)
    builder.setPersisted(true)

    const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
    const service = jobScheduler.schedule(builder.build());
    console.log(`Job Scheduled: ${jobScheduler.schedule(builder.build())}`);
  },

and in my AndroidManifest.xml I have:

<service android:name="org.nativescipt.ITPalert.TestJobService"
                    android:permission="android.permission.BIND_JOB_SERVICE"
                    android:enabled="true"
                    android:exported="false" />

I registered the job in the webpack.config.js:

  const appComponents = [
    "tns-core-modules/ui/frame",
    "tns-core-modules/ui/frame/activity",
    resolve(__dirname, "app/Jobs/TestJobService.js"),
 ];

And I got this error:

System.err: com.tns.NativeScriptException:
System.err: Calling js method onClick failed
System.err:
System.err: Error: java.lang.IllegalArgumentException: No such service ComponentInfo{org.nativescript.ITPalert/org.nativescipt.ITPalert.TestJobService}
System.err:     android.os.Parcel.readException(Parcel.java:1963)
System.err:     android.os.Parcel.readException(Parcel.java:1905)
System.err:     android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:180)
System.err:     android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:44)
System.err:     com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)
System.err:     com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:1070)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:1050)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:1042)
System.err:     com.tns.gen.java.lang.Object_vendor_30697_26_ClickListenerImpl.onClick(Object_vendor_30697_26_ClickListenerImpl.java:17)
System.err:     android.view.View.performClick(View.java:6896)

I built my project with --bundle and --hmr.


Solution

  • I needed to run: tns platform clean android, because my AndroidManifest.xml wasnt propagated in the build...

    I leave the question here, if I can help someone.