2

via the docs: https://facebook.github.io/react-native/docs/native-modules-android.html

Upon calling the method to access and thus use the NativeModule, it is not found...

'use strict';
import {NativeModules, DeviceEventEmitter} from 'react-native'

const MessengerModule = NativeModules.MessengerModule
const Messenger = {};

Messenger.show = (message, duration) => {
  console.log(NativeModules.MessengerModule) //undefined
  MessengerModule.show(message, duration);
};

module.exports = Messenger

undefined logged... Along with a nice little error:

ReactNativeJS: undefined is not an object (evaluating 'MessengerModule.show')

Obviously, not exactly the outcome I was looking for.

The Java Module:

package com.app.firebase;

import android.widget.Toast;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.util.Map;

public class MessengerModule extends ReactContextBaseJavaModule {

  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";

  public MessengerModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "MessengerModule";
  }

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
  }
}

The Java Package:

package com.app.firebase;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MessengerPackage implements ReactPackage {
  @Override
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new MessengerModule(reactContext));
    return modules;
  }

  @Override
  public List<Class<? extends JavaScriptModule>> createJSModules() {
    return Collections.emptyList();
  }

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }
}

Can someone please help me find the error in the code, or possibly what is missing and thus make the module functional? As always any direction and or point of reference is greatly appreciated!

studiobrain
  • 1,135
  • 2
  • 13
  • 35
  • Well, Nevermind. I added the `new MessengerPackage()` to `MainApplication` vs `MainActivity` and it is now found... Maybe update the docs? – studiobrain Aug 01 '16 at 05:07
  • In react-native pre 0.29 your need to add the package to MainActivity.java, and MainApplication.java in 0.29+ – Xeijp Aug 02 '16 at 02:41
  • after wasting one day i can't not any solution. for this problem can anyone solve this.... – Akash Kumar Feb 28 '19 at 08:04

0 Answers0