React-native-push-notification: evaluating RNPushNotification.getInitialNotification is not an object

Created on 6 Mar 2020  路  27Comments  路  Source: zo0r/react-native-push-notification

Hi all,

I have installed React native push Notification and following the installation guide correctly but I faced this error: TypeError: null is not an object (evaluating 'RNPushNotification.getInitialNotification ').

import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "@react-native-community/push-notification-ios";
import {Platform} from "react-native";
NotifService.js:

export default class NotifService {
  configure = () => {
    PushNotification.configure({
      // (optional) Called when Token is generated (iOS and Android)
      onRegister: function(token){
        console.log("[Notification Service] on register token:", token);
      },

      // (required) Called when a remote or local notification is opened or received
      onNotification: (notification) => {
        console.log("[Notification Service] onNotification:", notification);

        // process the notification

        // required on iOS only (see fetchCompletionHandler docs: https://github.com/react-native-community/react-native-push-notification-ios)
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
      senderID: "----",

      permissions: {
          alert: true,
          badge: true,
          sound: true,
      },
      popInitialNotification: true,
      requestPermissions: true,
    });
  }
}

MyComponent.js:

componentDidMount() {
    this.localNotif = new NotifService();
    this.localNotif.configure();
  }

Any help/Ideas please?!

Most helpful comment

Hello, I have the same error with an android device on an Expo App :

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

Does anyone has already fixed this problem ? Thank you!

All 27 comments

Wait for a fix or just downgrade. I have
"react-native-push-notification": "^3.1.1",
and this works

@GeldgeilerGnom, popInitialNotification returns always null, is it related to this problem?

hi @GeldgeilerGnom, thanks for your reply, I didn't test it on android but for the moment I'm using iOS and I'm facing this issue:

/Users/Me/Desktop/IZI/myProject/ios/build/myProject/Build/Products/Debug-iphoneos/libRCTPushNotification.a(RCTPushNotificationManager.o)
ld: 1 duplicate symbol for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

getInitialNotification does not work in dev debug, to achieve it disable debug. Also there are some issues, this library does not work properly and stop working after a while, the only way to make it work was to use the @react-native-community/push-notification-ios itself without this library and also the library by wix also does not work properly(it does not support background notifications, and somehow notifications stop working after a while).

import {
  AppState,
} from 'react-native';

import PushNotificationIOS from "@react-native-community/push-notification-ios";

const _removeAllListeners = () => {
  console.log('_removeAllListeners');
  // PushNotificationIOS.removeEventListener('notification');
  // PushNotificationIOS.removeEventListener('register');
  // PushNotificationIOS.removeEventListener('registrationError');
};

const NotificationService = {
  configure: ()  => {  // on notification
    console.log('NotificationService.configure');

    // _removeAllListeners();

    PushNotificationIOS.addEventListener('notification', (notification)=> {
      // const result = `Message: ${notification.getMessage()};\n
      //   badge: ${notification.getBadgeCount()};\n
      //   sound: ${notification.getSound()};\n
      //   category: ${notification.getCategory()};\n
      //   content-available: ${notification.getContentAvailable()}.`;
      // console.log(result);

      console.log('Push Notification Received');
      console.log(notification);

      let isFromBackground = AppState.currentState === 'background';

      console.log('foreground ' + !isFromBackground);
      console.log('userInteraction ' + isFromBackground);

    });
  },

  requestPermissions: () => {
    console.log('NotificationService.requestPermissions');

    PushNotificationIOS.addEventListener('register', (deviceToken)=> {
      console.log(`Registered For Remote Push Device Token: ${deviceToken}`);
    });

    PushNotificationIOS.addEventListener('registrationError', (error)=> {
      console.log(`Failed To Register For Remote Push Error (${error.code}): ${error.message}`);
    });

    PushNotificationIOS.requestPermissions().then((permissions)=> {
      console.log('PushNotificationIOS.requestPermissions');
      console.log(permissions);
    }).catch((err)=> {
      console.log('error: PushNotificationIOS.requestPermissions');
      console.log(err);
    });

  },

  abandonPermissions: () => {
    console.log('NotificationService.abandonPermissions');
    PushNotificationIOS.removeAllDeliveredNotifications()
    PushNotificationIOS.abandonPermissions();
    PushNotificationIOS.setApplicationIconBadgeNumber(0)
  },

  popInitial: () => {
    console.log('NotificationService.popInitial'); // does not work in dev debug, to achieve it disable debug
    PushNotificationIOS.getInitialNotification().then((notification) => {
      console.log("Initial notification was");
      console.log(notification);

      if (notification) {
        alert('get initial notification');
        alert(JSON.stringify(notification));
      }

    }).catch((err)=> {
      console.log('error: PushNotificationIOS.getInitialNotification');
      console.log(err);
    });

  },
};

export default NotificationService;

AppDelegate.m

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
 {
   // completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
   completionHandler(UNNotificationPresentationOptionNone); // mute(hide) notifications from Notification Center if received foreground.
 }

Hello, I have the same error with an android device on an Expo App :

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

Does anyone has already fixed this problem ? Thank you!

@Shamzic you find a solution ? I have the same error too

Me too :((

@Kasmadei Did you find the solution?

EDIT: This is most likely because you changed non hot loaded files when configuring (like manifest, .java files) without relaunching run-android.

Kill everything. Then configure everything as dictated by the github install process. Then start your react service 'npx react-native start' then run your android 'npx react-native run-android'

Still having a same error please did someone get a solution?

RNCLI

I solved my issue

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

Still having issues with this. Is there any solution or workaround for this ?

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()
      );
    }

Still not fixed? I am on version 7 and it is returning same error.

Hi @johnW6,

Still not provided a reproductible example, how do you expect this to be fixed ?
This is working well on the exemple project and other personnal and professionnal projects.

Regards,

147

318

1557

I was previously struggling with an issue where I could'nt see my action buttons and then planned to upgrade the version to latest version and now it's showing

image

The github repo for my project is here

I need your most valuable help asap due to my tight deadline. Hope I get a good reply from you !

Hi @tarunkishore2303
Did you follow the changelog ?
https://github.com/zo0r/react-native-push-notification/blob/master/CHANGELOG.md#600-2020-09-26

  • @react-native-community/push-notification-ios is now a peerDependency, please make sure that you installed this library with NPM or YARN.

Is that necessary for android ?

Did you tried: #1335 (comment) ?

Yeah tried that too !

Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓

Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !

The possible fix for this issue is

_reset the cache and delete the build folder and now the error will be gone_

Did you tried: #1335 (comment) ?

Yeah tried that too !

Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓

Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !

The possible fix for this issue is

_reset the cache and delete the build folder and now the error will be gone_

@tarunkishore2303 where is the cache and build folder? I have this error! thanks

Did you tried: #1335 (comment) ?

Yeah tried that too !

Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓

Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !

The possible fix for this issue is

_reset the cache and delete the build folder and now the error will be gone_

this woked for me

npm start -- --reset-cache

Hello, I have the same error with an android device on an Expo App :

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

Does anyone has already fixed this problem ? Thank you!

did you fix this? how???

Did you tried: #1335 (comment) ?

Yeah tried that too !

Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓

Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !

The possible fix for this issue is

_reset the cache and delete the build folder and now the error will be gone_

@tarunkishore2303 where is the cache and build folder? I have this error! thanks

Build folder's location : android/app/build

And you don't need to manually search for the cache and delete it. Jus run the command npm start -- --reset-cache in your project's directory

Did you tried: #1335 (comment) ?

Yeah tried that too !
Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓
Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !
The possible fix for this issue is
_reset the cache and delete the build folder and now the error will be gone_

@tarunkishore2303 where is the cache and build folder? I have this error! thanks

Build folder's location : android/app/build

And you don't need to manually search for the cache and delete it. Jus run the command npm start -- --reset-cache in your project's directory

Hello there!

Unfortunately, this solution doesn't work for me.

I'm using the 7.2.3 version

Did you tried: #1335 (comment) ?

Yeah tried that too !
Edit : I reset the cache and deleted the build folder and now the error is gone but I am not getting the notification 馃槓
Edit 2. : I forgot to create a channel and after reading the changelog I did it properly and now it works perfectly and fine !
The possible fix for this issue is
_reset the cache and delete the build folder and now the error will be gone_

@tarunkishore2303 where is the cache and build folder? I have this error! thanks

Build folder's location : android/app/build

And you don't need to manually search for the cache and delete it. Jus run the command npm start -- --reset-cache in your project's directory

Hello there!

Unfortunately, this solution doesn't work for me.

I'm using the 7.2.3 version

I would suggest you to uninstall and re install the app on your device after clearing cache and deleting build folder. I assume this issue is being caused when react native doesn't recognise it properly.

@tarunkishore2303 Thank you very much!!! That really works!

Was this page helpful?
0 / 5 - 0 ratings