Skip to content

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

@nehab5889

Description

@nehab5889

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions