react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

TypeError: null is not an object (evaluating 'RNPushNotification.getInitialNotification')

Open hrahul2605 opened this issue 6 years ago • 4 comments

Bug

Hi everyone, I am getting this error but in a different manner. I am using this package to generate local push notifications without help of firebase.

If i run npx react-native run android i.e. in debugging mode, i get no such errors, and everything is working totally fine.

Now again as this is a expo initialised project, and if i do expo start , i get this error message.

I thought maybe its just some error with Expo. So i tried to build my apk with npx react-native run android --variant=release with keystore and everything generated properly.

But sadly my release apk keeps on crashing. In both methods, in expo build:android as well as npx react-native run android --variant=release I dont know what is causing this kind of errors. Release apks always keeps crashing.

Please suggest me some way to overcome this, or any other way so that i could push local notification.

Environment info

System:
    OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 1.14 GB / 7.66 GB
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 12.18.2 - ~/.nvm/versions/node/v12.18.2/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.5 - ~/.nvm/versions/node/v12.18.2/bin/npm
    Watchman: 4.9.0 - /usr/bin/watchman
  SDKs:
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 1.8.0_252 - /usr/bin/javac
    Python: Not Found
  npmPackages:
    @react-native-community/cli: Not Found
    react: ~16.11.0 => 16.11.0 
    react-native: ~0.62.2 => 0.62.2 
  npmGlobalPackages:
    *react-native*: Not Found

PushNotification.configure({
    onRegister: function (token) {
      console.log("TOKEN:", token);
    },
    onNotification: function (notification) {
      console.log("NOTIFICATION:", notification);
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    },

    onAction: function (notification) {
      console.log("ACTION:", notification.action);
    },
    largeIcon: "ic_launcher",
    smallIcon: "ic_notification",
    permissions: {
      alert: true,
      badge: true,
      sound: true,
    },
    popInitialNotification: true,
    requestPermissions: Platform.OS === "ios",
  });

MainApplication.java

import android.app.Application;
import android.content.Context;
import android.net.Uri;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.todo.generated.BasePackageList;
import com.airbnb.android.react.lottie.LottiePackage;

import org.unimodules.adapters.react.ReactAdapterPackage;
import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.Package;
import org.unimodules.core.interfaces.SingletonModule;
import expo.modules.constants.ConstantsPackage;
import expo.modules.permissions.PermissionsPackage;
import expo.modules.filesystem.FileSystemPackage;
import expo.modules.updates.UpdatesController;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;

public class MainApplication extends Application implements ReactApplication {
  private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(
    new BasePackageList().getPackageList()
  );

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
      packages.add(new LottiePackage());
      // NOTE: adding the line `packages.add(new ReactNativePushNotificationPackage())` throws error that it is overwritting this package
      return packages;
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }

    @Override
    protected @Nullable String getJSBundleFile() {
      if (BuildConfig.DEBUG) {
        return super.getJSBundleFile();
      } else {
        return UpdatesController.getInstance().getLaunchAssetFile();
      }
    }

    @Override
    protected @Nullable String getBundleAssetName() {
      if (BuildConfig.DEBUG) {
        return super.getBundleAssetName();
      } else {
        return UpdatesController.getInstance().getBundleAssetName();
      }
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);

    if (!BuildConfig.DEBUG) {
      UpdatesController.initialize(this);
    }

    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }

  /**
   * Loads Flipper in React Native templates. Call this in the onCreate method with something like
   * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
   *
   * @param context
   * @param reactInstanceManager
   */
  private static void initializeFlipper(
      Context context, ReactInstanceManager reactInstanceManager) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.rndiffapp.ReactNativeFlipper");
        aClass
            .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
            .invoke(null, context, reactInstanceManager);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

build.gradle

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
    implementation project(':lottie-react-native')
    implementation project(':react-native-push-notification')
    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }
    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }
    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }
    addUnimodulesDependencies()

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

package.json

{
  "main": "index.js",
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "expo start --web",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/native": "^5.6.1",
    "@react-navigation/stack": "^5.6.2",
    "@types/redux-persist": "^4.3.1",
    "expo": "~38.0.1",
    "expo-font": "~8.2.1",
    "expo-screen-orientation": "^1.1.1",
    "expo-splash-screen": "^0.3.1",
    "expo-status-bar": "^1.0.0",
    "expo-updates": "~0.2.8",
    "lottie-react-native": "^3.4.0",
    "moment-jalaali": "^0.9.2",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-native": "~0.62.2",
    "react-native-gesture-handler": "~1.6.0",
    "react-native-push-notification": "^4.0.0",
    "react-native-reanimated": "^1.9.0",
    "react-native-redash": "^14.2.1",
    "react-native-safe-area-context": "~3.0.7",
    "react-native-screens": "^2.9.0",
    "react-native-svg": "^12.1.0",
    "react-native-unimodules": "~0.10.0",
    "react-native-web": "~0.11.7",
    "react-redux": "^7.2.0",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0",
    "@types/react": "~16.9.23",
    "@types/react-dom": "~16.9.8",
    "@types/react-native": "~0.61.23",
    "babel-preset-expo": "~8.2.0",
    "jest-expo": "~38.0.0",
    "react-native-svg-transformer": "^0.14.3",
    "typescript": "~3.9.5"
  },
  "jest": {
    "preset": "react-native"
  },
  "private": true
}

hrahul2605 avatar Jul 24 '20 09:07 hrahul2605

possible duplicate https://github.com/zo0r/react-native-push-notification/issues/1335

acSpock avatar Aug 22 '20 19:08 acSpock

#1335

hrahul2605 avatar Aug 27 '20 05:08 hrahul2605

I solved my issue

npm install react-native-push-notification --save

michaelVictoriaDev avatar Oct 16 '20 09:10 michaelVictoriaDev

The package would not be linked to your java file. In your MainApplication.java file add:

import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

also add new ReactNativePushNotificationPackage() to getPackages method return value so it looks like this :

 @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
              new ReactNativePushNotificationPackage()
      );
    }

anonewb avatar Nov 05 '20 04:11 anonewb