React-native-push-notification: undefined is not an object (evaluating 'RNPushNotification.presentLocalNotification')

Created on 19 Dec 2016  路  28Comments  路  Source: zo0r/react-native-push-notification

Hello,

I setup react-native-push-notification according the Readme, but when i use the localNotification like this:

var PushNotification = require('react-native-push-notification');

PushNotification.localNotification({
   message: 'My notification message'
});

I have this error:

undefined is not an object (evaluating 'RNPushNotification.presentLocalNotification')

Have you some idea about this ?

My react native version: 0.36

Most helpful comment

Apparently i have the same problem, with a different message: undefined is not an object (evaluating RNPushNotification.getInitialNotification)

Any ideas?

All 28 comments

sounds like you've not _linked_ you're project correctly. Please recheck the readme and the trouble shooting guide.

I was having this problem just a minute ago. i did react-native link and the output said the notification lib was linked.. but I needed to modify MainActivity anyway.

Always the same :/
I just recheck readme and trouble shooting guide, i do react-native link, and in settings.gradle :

include ':react-native-push-notification'
project(':react-native-push-notification').projectDir = file('../node_modules/react-native-push-notification/android')

in build.gradle:

    compile project(':react-native-push-notification')
    compile ('com.google.android.gms:play-services-gcm:+') {
        force = true;
    }

In the MainApplication.java the package is imported

import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

and i verified that package has been added in react package list

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

Also, i run the application in my physical devise and i sure that this package are correctly linked.

Do you think it is surely a version issue ?

Did you also check your manifest file?

Yes, here is my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
       android:name="com.myapp.permission.C2D_MESSAGE"
       android:protectionLevel="signature" />
    <uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme">

      <receiver
          android:name="com.google.android.gms.gcm.GcmReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.myapp" />
          </intent-filter>
      </receiver>

      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
         <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
         </intent-filter>
      </receiver>
      <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
      <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
      </service>
      <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

      <meta-data
       android:name="com.google.android.geo.API_KEY"
       android:value="--mykey--" />
    </application>

</manifest>

Just a question in passing, if i want use only local notification, do i still to set GCM part in manifest file ?

i had the same issue. Fixed after running react-native run-android in the terminal.

Apparently i have the same problem, with a different message: undefined is not an object (evaluating RNPushNotification.getInitialNotification)

Any ideas?

@flaviosbert I just had that issue and it appeared react-native link did not link the dependency because it was not listed as a dependency in package.json.

To fix this, make sure you install the package with the --save flag:

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

@npomfret If you accept pull requests, I'd be happy to make one to update the documentation with the npm --save flag

I can merge PRs. I don't have much time to test, but a change to the docs won't need testing. Thanks for your input

I have the same issue

I'm struggling with the same here. I have ran the npm install with --save flag, react-native link and connected everything manually as well.

Did you manage to get it to work eventually @nicolaselmir @Dynamique-Zak ? If yes, what was the problem?

react-native link and react-native run-* should solve the issue.
I did this when I had faced the same issue.

Can confirm running react-native run-android after react-native link command solves the issue

Just had the same issue, it was because react-native link did not add new ReactNativePushNotificationPackage() in MainApplication.java, adding it manually made it work.

I had the same issue. After couple of hours of troubleshooting, I realised that I had a redundant package which was being imported right after

import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;

in MainApplicaiton.java.

Make sure all packages imported in MainApplicaiton.java are being used and do exist in the project.

Hey ! With long delay, i'll close this issue, sorry for the wait, but it's only recently that I've been able to test your suggestions,
Thanks !

After running react-native link, I received this error:

Dex: Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
        at com.android.dx.command.dexer.Main.run(Main.java:277)
        at com.android.dx.command.dexer.Main.main(Main.java:245)
        at com.android.dx.command.Main.main(Main.java:106)

:app:transformClassesWithDexForDebug FAILED

Update: I had to add the following into my build.gradle file:

compile ("com.google.android.gms:play-services-base:10.0.1") {
        force = true;
    }
    compile ("com.google.android.gms:play-services-maps:10.0.1") {
        force = true;
    }
    compile ("com.google.android.gms:play-services-gcm:10.0.1") {
        force = true;
    }
    compile ('com.google.firebase:firebase-core:10.0.1') {
        force = true;
    }
    compile ('com.google.firebase:firebase-messaging:10.0.1') {
        force = true;
        }

but now it gives me the same error:
undefined is not an object (evaluating RNPushNotification.getInitialNotification)

Doesn't seem like this issue should be closed...

Facing the same issue as @BobAleena . Got any solution?

Am having the same issue "undefined is not an object (evaluating RNPushNotification.getInitialNotification)"

Please any solution ?

Hey guys, any updates on this?

The issue I faced- "undefined is not an object (evaluating RNPushNotification.getInitialNotification)".
The reason- I was trying to integrate the library while using expo for running project which is not supported. You will have to eject your project using "react-native init". Doing so worked for me.
Strange thing is no one has mentioned that it will not work with expo as a lot of people use expo for development of their mobile apps.

I'm using expo ejected app , also got this error, Is that true this package not supporting expo apps? Any idea? Tried all the ways mentioned above, still got the error.

If I run using react-native run-android I get an error about permissions. Not sure if it's related, but I get the above errors if I run using exp start. Either way I get errors, it does seem as though Expo is not supported even after ejecting the app.

I'm having same issue "undefined is not an object (evaluating 'rnonesignal');"
Any solution ??? plz reply.

Make sure you import it like this import PushNotification from 'react-native-push-notifications'; (no brackets). Or whatever the OneSignal class is (I'm not using it for OneSignal, so I'm not sure how that works in this library). Also just follow the setup documentation slowly and carefully, if you follow all of the steps it will work. If it still doesn't work, just post your manifest here and your JavaScript and someone will identify the issue.

It does work in Expo ejected apps, but only if you do a full release. It won't work (in my experience) if you run it in an Expo debug build (it's a debug build if the script bundler is running). Unfortunately, despite React Native recommending using Expo, it ends up breaking compatibility with a lot of libraries and with the React Native tools themselves, so I'm pretty sorry for anyone (including myself) that went that route.

Yes, here is my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
       android:name="com.myapp.permission.C2D_MESSAGE"
       android:protectionLevel="signature" />
    <uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme">

      <receiver
          android:name="com.google.android.gms.gcm.GcmReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.myapp" />
          </intent-filter>
      </receiver>

      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
         <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
         </intent-filter>
      </receiver>
      <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
      <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
      </service>
      <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

      <meta-data
       android:name="com.google.android.geo.API_KEY"
       android:value="--mykey--" />
    </application>

</manifest>

Just a question in passing, if i want use only local notification, do i still to set GCM part in manifest file ?

Yes, here is my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
       android:name="com.myapp.permission.C2D_MESSAGE"
       android:protectionLevel="signature" />
    <uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme">

      <receiver
          android:name="com.google.android.gms.gcm.GcmReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.myapp" />
          </intent-filter>
      </receiver>

      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
         <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
         </intent-filter>
      </receiver>
      <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
      <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
      </service>
      <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

      <meta-data
       android:name="com.google.android.geo.API_KEY"
       android:value="--mykey--" />
    </application>

</manifest>

Just a question in passing, if i want use only local notification, do i still to set GCM part in manifest file ?

I also want to know if it's necessary to config in android/build.gradle and AndroidManifest.xml file when i only use local notification, thx for reply anybody!

react-native run-android solves this for me too like @edmondcang suggested.

Was this page helpful?
0 / 5 - 0 ratings