NFC using foregroundDispatch in react-native and android - react-native

I'm trying to develop an app in react-native that should use android's foreground dispatch system to intercept nfc events before any other activity does.
The android part is not a problem, I have done it in a native app.
What would the correct way of doing this be in an app that uses react-native?

I ended up adding creation of pending intent and adding of tag/NDEF filters to MainActivity. Then onNewIntent parses the relevant data and relays it to js via
getReactInstanceManager()
.getCurrentReactContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("nfcTagEvent", params);
Then, in my main RN component
DeviceEventEmitter.addListener('nfcTagEvent', event =>
// do something with event
);

Related

Is there a way to create and dispatch/trigger custom event in React-Native?

What I have
I am trying to adapt a web designed library to react native, this implies that certain things must be replaced like the use of custom events, I want to implement this behaviour, create and dispatch/trigger custom events in RN.
I have reviewed some answers to similar questions, but there are some options to simulate custom events that are no longer works by the new react-native versions, since React Native doesn’t include the Node Standard Library.
What I want
this is a piece of code in which custom events are implemented and captured, this is what I want to simulate in RN.
const authEvent = new Event('auth');
// Listen for the event.
window.addEventListener('auth', function (e) { /* ... */ }, false);
// Dispatch the event.
el.dispatchEvent(authEvent);

React native Rest API call in background process when app in foreground

In React native., How to call REST API on background process when move from one screen to another and also get individual reference of called API when back to that screen to showcase the status of sync API and it will work when application in foreground.
I recommend to use state management like redux to access data from anywhere in your app
and take a look to this liperares
react-native-background-fetch
react-native-background-task

How to read data in background in React Native

I have an app in React Native, that uses the bluetooth connection to read data from external devices.
I need a way to continue to read this data in background when, for example, the user starts a reading session and put the app in background.
What should I use to do this?
My code is divided in two parts:
Scan and Connect
Reading Data from external devices.
You need a background service for this. The following link for Android will help you.
github => react-native-foreground-service
If you want, you can do it yourself as described on the RN official site. But you'll have to write Java code for it.
React Native => Native Module

Sending events from JS to Native (Java)

I'm using react native for an sdk. I need to be able to send an event from JavaScript to the native layer when a particular action has occurred.
The docs mention that it's possible to send events to JS, but I need to be able to send events in the other direction.
Any ideas?

pass a javascript function onto native

Is it possible to pass a javascript function from React Native onto iOS Native components such as a UIButton and execute there?
It is possible, but using events and not by sending JS to native components. Check out react-native docs for detailed information, but generally:
React Native enables you to perform cross-language function calls. You
can execute custom native code from JS and vice versa. Unfortunately,
depending on the side we are working on, we achieve the same goal in
different ways. For native - we use events mechanism to schedule an
execution of a handler function in JS, while for React Native we
directly call methods exported by native modules.