Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceEventEmitter.addListener is not working in Release build for Android. #43651

Open
nehab5889 opened this issue Mar 26, 2024 · 6 comments
Open
Labels
Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Newer Patch Available Platform: Android Android applications.

Comments

@nehab5889
Copy link

nehab5889 commented Mar 26, 2024

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

@github-actions github-actions bot added Newer Patch Available Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Mar 26, 2024
Copy link

⚠️ Newer Version of React Native is Available!
ℹ️ 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.

Copy link

⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

@github-actions github-actions bot added the Platform: Android Android applications. label Mar 26, 2024
@cortinico
Copy link
Contributor

Please provide a repro

@nehab5889
Copy link
Author

nehab5889 commented Mar 27, 2024

@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());
}

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Mar 27, 2024
@cortinico
Copy link
Contributor

@nehab5889 can you use the linked template provided here:
#43651 (comment)

@Hariprakash010501
Copy link

When i try to use setState inside DeviceEventEmitter.addListener, its crashing the app (Maximum depth exceeded) in react native 0.72.10

@cortinico cortinico added Needs: Author Feedback and removed Needs: Attention Issues where the author has responded to feedback. labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Newer Patch Available Platform: Android Android applications.
Projects
None yet
Development

No branches or pull requests

3 participants