Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Headless JS in react native android application?

I need implement Headless JS in my React Native Android Application, but I got following issue :

enter image description here

Here is My Code :

index.android.js :

import React, { Component } from 'react';
import {AppRegistry} from 'react-native';
import SomeTaskName from './SomeTaskName'
AppRegistry.registerComponent('SomeTaskName', () => SomeTaskName);

SomeTaskName.js

module.exports = async (taskData) => {
   alert('test');
}

MyTaskService.java

import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
public class MyTaskService extends HeadlessJsTaskService {
   @Override
   protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
   Bundle extras = intent.getExtras();
     if (extras != null) {
        return new HeadlessJsTaskConfig(
          "MyTaskService",
          Arguments.fromBundle(extras),
         5000);
     }
     return null;
  }
}

and add service in AndroidManifest.xml

<service android:name=".MyTaskService" android:enabled="true" android:label="MyTaskService" />

Please , Anyone help to fix this issue.

like image 904
Saravana Kumar Avatar asked Mar 24 '17 12:03

Saravana Kumar


People also ask

What is headless JS in react-native?

Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.

Can we build Android app using react JS?

Supports Cross-Platform Application Development: By using React JS, you can make applications for a variety of platforms, including iOS and Android platforms. Therefore, you no longer have to learn different languages, like C++ and Java.

How do I run API in the background in react-native?

You can use the setInterval and setTimeout functions. This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.

Is it possible to run a react-native app in the background?

React Native background service library for running background tasks forever in Android & iOS. Schedule a background job that will run your JavaScript when your app is in the background or foreground.


1 Answers

You have to register a headless task. So replace the following code:

AppRegistry.registerComponent('SomeTaskName', () => SomeTaskName);

with:

AppRegistry.registerHeadlessTask('SomeTaskName', () => SomeTaskName);

And you need to call the task from your service, so replace:

if (extras != null) {
    return new HeadlessJsTaskConfig(
      "MyTaskService",
      Arguments.fromBundle(extras),
     5000);
 }
 return null;

with:

// Following line just to be sure it does not silently fail
WritableMap data = extras != null ? Arguments.fromBundle(extras) : null;
return new HeadlessJsTaskConfig(
    "SomeTaskName", // Use the registered headless Task here
    data,
    5000);
like image 192
NoilPaw Avatar answered Oct 22 '22 13:10

NoilPaw