Pushwoosh / pushwoosh-react-native-plugin

Other
57 stars 43 forks source link

[Bug]: TypeError: Cannot read property 'init' of null #165

Closed favalosdev closed 3 weeks ago

favalosdev commented 1 month ago

Documentation

Description

When trying to call methods such asPushwoosh.init,Pushwoosh.getHwid(), Pushwoosh.addTags, sometimes the following error arises: TypeError: Cannot read property 'init' of null

Bug severity

Normal

Steps to Reproduce

  1. Initialize a new Expo react-native project with the following dependencies: "react-native": "0.74.1", "expo": "51.0.8", "pushwoosh-expo-plugin": "1.0.0" & "pushwoosh-react-native-plugin": "6.1.31"

  2. Try and run any of the following methods: Pushwoosh.init, Pushwoosh.getHwid, Pushwoosh.addTags

Your Pushwoosh React Native Plugin version

6.1.31

Last worked Pushwoosh React Native Plugin version (if any)

No response

Affected platforms

Affected OS versions and/or devices

Workaround

No response

Relevant log output

No response

enginseer-dev commented 1 month ago

The issue you're describing is related to the Pushwoosh SDK not being properly linked to the React Native Android project. Given the error description, the problem seems to be that the native Pushwoosh module isn't being recognized in the React Native environment.

May I ask you to check the following:

  1. Check Auto-Linking. Confirm that pushwoosh-react-native-plugin has been added to android/settings.gradle and android/app/build.gradle. If the linking was failed, please rebuild the Android project.
  2. Sometimes, the React Native cache might cause issues. Clear the cache and try running the app again.
  3. Ensure you're initializing Pushwoosh correctly. The error you're seeing might be due to attempting to call the .init method before the module is ready. Ensure your code resembles:
    
    import Pushwoosh from 'pushwoosh-react-native-plugin';

Pushwoosh.init({ "pw_appid" : "YOUR_PUSHWOOSH_APP_ID" , "project_number" : "YOUR_FCM_SENDER_ID" });


4. It might be possible that there's a compatibility issue with the versions of React Native and Pushwoosh SDK. 
Ensure you're using the [latest version](https://github.com/Pushwoosh/pushwoosh-react-native-plugin/releases) of the Pushwoosh plugin and see if the issue persists. The latest version is 6.1.32

If you're still facing the issue, upload the full stack trace of the error.
enginseer-dev commented 1 month ago

@favalosdev Can you please provide a detailed, step-by-step description of how you are running the application? This will help us better understand the context and troubleshoot the issue. Thank you!

enginseer-dev commented 1 month ago

@favalosdev, we partially reproduced this issue, but it depends on the certain behavior. May I ask you to clarify us how do you build and launches the android project?

For example, in Expo, if you run the project with the npx expo start command, you may get an error, but if you launch the project with the npx expo run:ios/android command, then there is no error.

Could you please provide us a reproducer app? (you also may send us a link to help@pushwoosh.com for privacy reasons)

enginseer-dev commented 3 weeks ago

Hello, @favalosdev

For an Expo app prebuild is required. Prebuild entails the generation of native runtime code for the project, resulting in the construction of the 'ios' and 'android' directories. Through prebuilding, the native modules that utilize CocoaPods, autolinking, and other configuration plugins are automatically linked and configured.

After running:

npx expo prebuild

You can build and run native projects:

# Build and run your native iOS project
npx expo run:ios

# Build and run your native Android project
npx expo run:android

Otherwise you may face the TypeError: Cannot read property 'init' of null error, because the needed native code wasn't generated.