react-native-notifications works on iOS, failed on Android

3.3k Views Asked by At

I've installed react-native-notifications and on iOS everything works perfectly, but it keeps giving me errors on Android. I've followed the exact Android installation instructions from here: https://wix.github.io/react-native-notifications/docs/installation-android

My dev flow:

  1. Make modification.
  2. npx react-native clean-project-auto; npx react-native-clean-project
  3. rm -rf android/app/build; anroid/gradlew clean; android/gradlew build --refresh-dependencies
  4. npx react-native start --reset-cache
  5. Android Studio Invalidate Caches/Restart.
  6. Android Studio clean build.
  7. Android Studio sync Gradle files and run app.
  8. npx react-native run-android --deviceId emulator-5554

Step 7. results in Invariant Violation: PushNotificationManager is not available. (image below)

Step 8. results in the following logs:

...

> Task :react-native-notifications:testReactNative60DebugUnitTest

...

com.wix.reactnativenotifications.core.InitialNotificationHolderTest > replacesInitialNotification PASSED

com.wix.reactnativenotifications.core.InitialNotificationHolderTest > isALazySingleton PASSED
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.robolectric.util.ReflectionHelpers$6 (file:/[path]/.gradle/caches/transforms-2/files-2.1/4e5696f3256b4082eea0964d812439f4/jetified-shadowapi-4.3.jar) to method java.lang.ClassLoader.getPackage(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.robolectric.util.ReflectionHelpers$6
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

com.wix.reactnativenotifications.core.notification.PushNotificationTest > onPostRequest_emptyData_postNotification FAILED
    java.lang.RuntimeException
        Caused by: java.lang.RuntimeException
            Caused by: java.lang.IllegalAccessException

...

When I add the package, packages.add(new RNNotificationsPackage(MainApplication.this)) to the MainApplication.java getPackages() method, it says this package has already been linked with this error: (image below)

E/unknown:ReactNative: Exception in native call
    java.lang.IllegalStateException: Native module RNBridgeModule tried to override RNNotificationsModule. Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExistingModule=true. This error may also be present if the package is present only once in getPackages() but is also automatically added later during build time by autolinking. Try removing the existing entry and rebuild.
        at com.facebook.react.NativeModuleRegistryBuilder.processPackage(NativeModuleRegistryBuilder.java:55)
        at com.facebook.react.ReactInstanceManager.processPackage(ReactInstanceManager.java:1298)
        at com.facebook.react.ReactInstanceManager.processPackages(ReactInstanceManager.java:1269)
        at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1194)
        at com.facebook.react.ReactInstanceManager.access$1000(ReactInstanceManager.java:132)
        at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:996)
        at java.lang.Thread.run(Thread.java:919)

Dependencies:

"@react-native-community/push-notification-ios": "^1.2.0"
"react-native-notifications": "^3.2.1",

app/build.gradle

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    implementation project(':react-native-notifications')
    implementation 'com.google.firebase:firebase-core:17.3.0'

    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation 'com.google.firebase:firebase-analytics:17.3.0'
    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
    }
}

android/build.gradle

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 29
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        classpath 'com.google.gms:google-services:4.3.3'  // Google Services plugin

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

enter image description here

enter image description here

I would really like to use this package because it works really well on iOS. Anything helps, thanks!

UPDATE: The work-around to this is the react-native-push-notification dependency. However, Step 8 above still results in the error if the react-native-notifications package is installed. I'm unsure if it is worthy of a bug report, or if there is a fix to enable Java permissions through my WebStorm CLI, or something of the sort.

1

There are 1 best solutions below

0
On

Got into the same error and luckily I was able to fix it by encapsulating the IOS part in Platform checks, like:

if(Platform.OS === 'ios'){
    PushNotificationIOS.removeAllDeliveredNotifications();
}