DeviceEventEmitter.addListener is not working in Release build for Android.
Description
When app is launched from quit state DeviceEventEmitter.addListener is not working in Release build for Android. It is working perfectly fine in Debug build. I have tried using both the methods:
const eventEmitter = new NativeEventEmitter(NativeModules.RNEventEmitter); eventEmitter.addListener('EventReminder', event => { console.log(event); }
OR
DeviceEventEmitter.addListener('EventReminder', event => { console.log(event); }
Android Side Code:
private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
void checkPayload(Intent intent) {
String action = intent.getAction();
if(action != null && !action.isEmpty() && action.equals("NOTIFICATION_PAYLOAD_READ")) {
String payload = intent.getStringExtra("NOTIFICATION_PAYLOAD");
ReactInstanceManager mReactInstanceManager = getReactNativeHost().getReactInstanceManager();
ReactContext context = mReactInstanceManager.getCurrentReactContext();
WritableMap params = Arguments.createMap();
params.putString("eventProperty", payload);
if(context == null){
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceEventListener() {
public void onReactContextInitialized(ReactContext validContext) {
// Use validContext here
sendEvent(validContext, "EventReminder", params);
mReactInstanceManager.removeReactInstanceEventListener(this);
}
});
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
// Construct it in the background
mReactInstanceManager.createReactContextInBackground();
}
}else {
// App in background
params.putBoolean("backgroundApp", true);
sendEvent(context, "EventReminder", params);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.show(this);
checkPayload(getIntent());
}
Could you please help with resolving this? I need to emit parameters from native android to react native
Steps to reproduce
const eventEmitter = new NativeEventEmitter( NativeModules.RNEventEmitter, ); eventEmitter.addListener('EventReminder', event => { console.log(event); }
OR DeviceEventEmitter.addListener('EventReminder', event => { console.log(event); }
React Native Version
0.73.5
Affected Platforms
Runtime - Android
Output of npx react-native info
System:
OS: macOS 14.1.1
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 1.12 GB / 16.00 GB
Shell:
version: 3.2.57
path: /bin/bash
Binaries:
Node:
version: 18.14.2
path: /usr/local/bin/node
Yarn:
version: 1.22.10
path: ~/.npm-global/bin/yarn
npm:
version: 9.6.0
path: ~/.npm-global/bin/npm
Watchman:
version: 2023.12.04.00
path: /usr/local/bin/watchman
Managers:
CocoaPods:
version: 1.12.1
path: /Users/nehabhandari/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 23.4
- iOS 17.4
- macOS 14.4
- tvOS 17.4
- visionOS 1.1
- watchOS 10.4
Android SDK: Not Found
IDEs:
Android Studio: 2023.2 AI-232.10227.8.2321.11479570
Xcode:
version: 15.3/15E204a
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.10
path: /usr/bin/javac
Ruby:
version: 2.7.5
path: /Users/nehabhandari/.rbenv/shims/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.73.5
wanted: 0.73.5
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: true
newArchEnabled: false
Stacktrace or Logs
-
Reproducer
Screenshots and Videos
No response
| :warning: | Newer Version of React Native is Available! |
|---|---|
| :information_source: | You are on a supported minor version, but it looks like there's a newer patch available - 0.73.6. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases. |
| :warning: | Missing Reproducible Example |
|---|---|
| :information_source: | We could not detect a reproducible example in your issue report. Please provide either:
|
Please provide a repro
@cortinico I have updated the issue and added the native side code too
Code on React native side:
useEffect(() => {
// deep link logic for fresh app launch
if (navPath?.path && isReady && isFirstLaunch) {
const { path, screen } = navPath;
checkIsFirstLaunchProp(false);
if (!isfetchingUserInfo) {
NavigationService.navigateToScreen(path, screen);
}
}
}
const eventEmitter = new NativeEventEmitter(
NativeModules.RNEventEmitter,
);
const checkNavigationPath = async (obj, event) => {
setNavigationState(obj, event);
};
// listener for notification
const eventListener = eventEmitter.addListener('EventReminder', event => {
const obj = event?.body;
checkNavigationPath(obj, event);
});
// Removes the listener once unmounted
return () => {
eventListener.remove();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isReady]);
Android side code:
private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
void checkPayload(Intent intent) {
String action = intent.getAction();
if(action != null && !action.isEmpty() && action.equals("NOTIFICATION_PAYLOAD_READ")) {
String payload = intent.getStringExtra("NOTIFICATION_PAYLOAD");
ReactInstanceManager mReactInstanceManager = getReactNativeHost().getReactInstanceManager();
ReactContext context = mReactInstanceManager.getCurrentReactContext();
WritableMap params = Arguments.createMap();
params.putString("eventProperty", payload);
if(context == null){
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceEventListener() {
public void onReactContextInitialized(ReactContext validContext) {
// Use validContext here
sendEvent(validContext, "EventReminder", params);
mReactInstanceManager.removeReactInstanceEventListener(this);
}
});
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
// Construct it in the background
mReactInstanceManager.createReactContextInBackground();
}
}else {
// App in background
params.putBoolean("backgroundApp", true);
sendEvent(context, "EventReminder", params);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.show(this);
checkPayload(getIntent());
}
@nehab5889 can you use the linked template provided here: https://github.com/facebook/react-native/issues/43651#issuecomment-2020155469
When i try to use setState inside DeviceEventEmitter.addListener, its crashing the app (Maximum depth exceeded) in react native 0.72.10
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.
This issue was closed because the author hasn't provided the requested feedback after 7 days.