React native AppState "change" event not triggered by Android 12 Task Manager - react-native

Our react-native 0.66 Android app has an AppState change handler which is called when the app goes into the background / foreground...
import {AppState} from 'react-native';
...
this.changeUnsubscribe = AppState.addEventListener('change', this.handleChange)
...
handleChange(nextAppState) { console.log(`App state changed to: ${nextAppState}`) }
Works perfectly on Android 10. However on Android 12, the change event isn't happening when the user brings up the Task Manager.
Instead the event happens when you swipe up the app in the Task Manager (to close the app).
Unfortunately that causes a problem: It seems Android 12 limits the amount of activity you can perform as the app is closing. We're trying to save user data when the app goes into the background, and it's not always managing it. Pre-Android 12 our app managed it fine.
I can't find any mention of this new behaviour in the react-native or Android docs. Any tips anyone?

Answering my own question, hope it helps others:
For react-native v0.66 on Android 12 (may be the case for other versions): The system Task Manager no longer triggers the AppState change event.
It now triggers an AppState blur event. Returning to the app triggers a focus event. The AppState currentState will continue to be active while Task Manager is in the foreground.

Related

Running cleanup when a react native app is closed

In my react-native app's code:
I am monitoring AppState changes
When AppState changes to 'inactive' or 'background', I write about 2000 key/value pairs of data to AsyncStorage
When the app starts I read this data
When I test the app on android (didn't test on iOS yet):
If I minimize the app, the data is written as expected. I can see that by closing the app after minimizing it, and restarting it
However, if I click on the 'Recent' button in the navigation bar (which changes AppState to 'inactive') and immediately click on the 'X' button at the top right of the app, the data doesn't seem to be written, or at least not all of it (I am not checking all 2000 values, just few of them). If I try to write only few values, they are written
Question:
The explanation to the above behavior seems to be simple: the app has enough time to perform few operations before I click on 'close', but not to write 2000 values. Is there a way to perform the writing before the app is closed?
Actually it's not recommended to do any async function or anything that could take too long. Because you don't have the control of how much time it will take to write the 2000 values. The app can go to background and the SO can kill the proccess of your app. When the app is in background is the SO that controls it.

How can i perform task even if the app is closed in React Native

I want to perform some task if the device is back online. I have tried with Broadcast receiver but its working perfectly when the app is running and the app is moved to background. but its not working when the app is closed :(.
please find the below code which i have tried..
import {DeviceEventEmitter,Platform} from 'react-native';
const func=(map)=>{
console.log("connection changed")
}
DeviceEventEmitter.addListener('GReferrerBroadcastReceiver', func);
Can anyone propose a solution for how to perform some task if the app is closed. I found Job scheduler in native android but how can i connect to React Native???
a help will be really appreciable :) ......
This library implement's background tasks. It's implementation is in JS and pretty easy to use: https://github.com/Rapsssito/react-native-background-actions
React-native-background-action works pretty well refer documentation from officially website but if u are looking for user tracking after start click then it fail after 10-15 minutes on phone lock...

NFC using foregroundDispatch in react-native and android

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
);

How to show an notification after successful app update via Code Push

I'm using code-push to update my react-native app. I do not want to update it every time via App Store.
I'm using installMode ON_NEXT_RESUME, thus the update is downloaded but not installed immediately. The new content will be available the next time the app is resumed or restarted, whichever event happens first. (if the app was inactive for 10 mins)
My code is as follows:
componentDidMount() {
codePush.sync({ installMode: codePush.InstallMode.ON_NEXT_RESUME,
minimumBackgroundDuration: 60 * 10 });
}
But the problem is than the app is updated but the user doesn't know it.
Is there any way to show an notification of something like that to user after successful update?
You can use
codePush.SyncStatus.UPDATE_INSTALLED
https://github.com/Microsoft/react-native-code-push/blob/master/docs/api-js.md#codepushsync

worklight what is the event fired when the application exit

Could you please help me to find the event or WL method fired when the worklight application exit ?
I have read the developer guide and may be i have miss it.
thanks.
regards,
jack
IBM Worklight ships with Apache Cordova, you can try to use Cordova's Event API. Specifically the pause event.
This is an event that fires when a Cordova application is put into the background.
//Add the event listener
function wlCommonInit() {
document.addEventListener("pause", onPause, false);
}
// Handle the pause event
function onPause() {
//...
}
When you quit the app, the app quits. Nothing else happens.
Otherwise, you'll need to edit your question and be clearer.
Do you mean:
when the app quits
how to programmatically quit the app
what happens when the app moves to the background
something else...?
Availability:
Android
Windows Phone 7.5
Windows Phone 8
Edit: based on the comment to this answer, you want to use WL.App.overrideBackButton, so that once tapping on the physical Back button instead of quitting the app you could display a WL.SimpleDialog or alert or whatever else you want to do. An 'OK' button could then trigger WL.App.close to quit the application (and a 'Cancel' button that will do nothing, to keep the user in the app).