ios: Invariant Violation: Native module cannot be null

At iOS, it builds successfully, however, when the app starts, I get this error:


Invariant Violation: Native module cannot be null.

constructor
    NativeEventEmitter.js:36:6
<global>
    PushNotificationIOS.js:17:32
loadModuleImplementation
    require.js:321:4
guardedLoadModule
    require.js:210:11
metroRequire
    require.js:128:6
module.exports.get__PushNotificationIOS
    react-native-implementation.js:257:11
<global>
    index.ios.js:10:13
loadModuleImplementation
    require.js:321:4
guardedLoadModule
    require.js:210:11


Any idea?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 9
  • Comments: 66

Most upvoted comments

For those who are having trouble getting this module on 0.61.5, please run pod install in your ios folder.

So solved this by adding manually @react-native-community/push-notification-ios tho it should be a dependency

How did you fix this?

ok, my solution was: I had an old version 0.59 and now I have 0.61.5 I used react-native-push-notification for both platforms my solution was

  1. remove the package, yarn remove react-native-push-notification
  2. delete last code in xcode appdelegate.m
/*#import <React/RCTPushNotificationManager.h>*/ - //here

#import <UserNotifications/UserNotifications.h>

@implementation AppDelegate

 /*- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
 {
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
 }
 // Required for the register event.
 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 {
  [RCTPushNotificationManager 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
 {
   [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
 }
 // Required for the registrationError event.
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
 {
  [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
 }
 // Required for the localNotification event.
 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
 {
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
 }*/ - //here

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"projectName"
                                            initialProperties:nil];
  1. install again, yarn add react-native-push-notification
  2. add in pod file this line pod 'RNCPushNotificationIOS', :path => '../node_modules/@react-native-community/push-notification-ios/RNCPushNotificationIOS.podspec'
  3. cd ios/ && pod install clear package and build in xcode again it works for me 😉

I am getting this same issue. not working even after installing from mainstream.

Same issue on 0.61.5. Only happens on iOS, Android is fine. Can’t figure out which package it is or how to debug this. Any suggestions?

Hi! I’m having the same issue. BUT I know what is the cause, it’s the package react-navigation-stack or react-navigation-tabs. They both use the package react-native-reanimated which is creating the following error for me.

image

I have tried everything that all of you already posted before me. And none of them have worked successfully, unfortunately…

If anyone has any idea how to fix this issue. You would really help me out big time.

I am only getting this error when running my test:

yarn run v1.22.4
$ jest __tests__/App1.test.tsx
 FAIL  __tests__/App1.test.tsx
  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.

      1 | import { createSlice, PayloadAction } from '@reduxjs/toolkit';
      2 | import { BackendNotificationInterface } from '../../services/api/PushNotificationSubscriber';
    > 3 | import PushNotificationIOS from '@react-native-community/push-notification-ios';
        | ^
      4 |
      5 | export interface StoredNotificationInterface {
      6 |   id: number;

      at invariant (node_modules/invariant/invariant.js:40:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:7)
      at Object.<anonymous> (node_modules/@react-native-community/push-notification-ios/js/index.js:18:33)
      at Object.<anonymous> (src/store/slices/NotificationsSlice.ts:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        32.126s
Ran all test suites matching /__tests__\/App1.test.tsx/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Any solution to this?

I am only getting this error when running my test:

yarn run v1.22.4
$ jest __tests__/App1.test.tsx
 FAIL  __tests__/App1.test.tsx
  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.

      1 | import { createSlice, PayloadAction } from '@reduxjs/toolkit';
      2 | import { BackendNotificationInterface } from '../../services/api/PushNotificationSubscriber';
    > 3 | import PushNotificationIOS from '@react-native-community/push-notification-ios';
        | ^
      4 |
      5 | export interface StoredNotificationInterface {
      6 |   id: number;

      at invariant (node_modules/invariant/invariant.js:40:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:7)
      at Object.<anonymous> (node_modules/@react-native-community/push-notification-ios/js/index.js:18:33)
      at Object.<anonymous> (src/store/slices/NotificationsSlice.ts:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        32.126s
Ran all test suites matching /__tests__\/App1.test.tsx/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Any solution to this?

Confirm. Have the same issue

@react-native-community/push-notification-ios

I had to do this before including the package to make it work:

  1. delete node_modules & package-lock.json
  2. npm cache clean --force
  3. npm install --save @react-native-community/push-notification-ios

Environment: react-native: 0.64.2 jest: 26.6.3 @react-native-community/push-notification-ios: 1.8.0

Fixed this issue today with the following:

// works
jest.mock('@react-native-community/push-notification-ios', () => ({
    addEventListener: jest.fn(),
    requestPermissions: jest.fn(),
  }));

It’s weird, you can comment out the return objects addEventListener and requestPermissions assignment and it continues to work, but if you try the following it doesn’t:

// doesn't work
jest.mock('@react-native-community/push-notification-ios');

Add it to your jest setup file and never worry about it again.

@radetsky that is exactly what I did and it resulted in this error message.

Something is very simple. @react-native-community/push-notification-ios": “1.0.3” linked with Podfile after install and link commands. Just cd to ios/ and run ‘pod install’. And restart the project.

FYI, for folks who are having issue using expo + expo-notifications. It seems like the lib is not ready for use yet.

image

I too am having the same issue as @netpoe. I have tried both “expo-notifications”: “~0.1.1” and “expo-notifications”: “^0.4.0”. I’m currently using “expo”: “~37.0.3”. As soon as I add “import * as Notifications from ‘expo-notifications’;” to a file the app won’t load on iOS simulator.

I guess we are all waiting for updates on this one =/

Any updates with this one?

Environment:

  • "expo": "^37.0.0",
  • "expo-notifications": "~0.1.1",

I’m getting this error when using expo-notifications as described in the expo-documentation:

Screen Shot 2020-07-03 at 00 05 52

I’m using a managed workflow. This happens in iOS Simulator iPhone 8. Could it be that it’s because I’m at the simulator? This error won’t happen in an Android device running the same app.

The code that’s causing this is:

React.useEffect(() => {
    Notifications.setBadgeCountAsync(2);
  }, [pendingPaymentRequestsCount]);

What I do get in Android is this warning:

photo_2020-07-03_00-09-35

Same error here with RN 59.10. I have patched above zo0r/react-native-push-notification@5b02761 manually. All code/setup steps in RN/XCode completed as per https://facebook.github.io/react-native/docs/pushnotificationios but no luck. Any ideas?

expo-notifications is a separate package from this repository. For any issues regarding the expo-notification package, please use the issue tracker for the expo-notification repository

I was having the same issues here after adding ‘expo-notifications’ to my project.

I fixed it by creating a brand new blank project using latest version of expo

expo i new-project

copying the files from my old project and reinstalling dependencies one by one as the compilation errors were presented. The first i’ve installed was ‘expo-notifications’ btw.

If you want to make sure ‘expo-notifications’ is working and test it just create the new project, install this dependency and copy the App.js content from the example

https://docs.expo.io/versions/latest/sdk/notifications/

Only then migrate the files from your old project.

In My case, I commented the use_native_modules! by mistake and that’s why some of the node modules having podspecs where not getting installed and was the real culprit. I just uncommented it and then pod install. It worked!

I got the same issue as @Wout-Vanneste-student described. https://github.com/react-native-community/react-native-push-notification-ios/issues/43#issuecomment-562862506

This is how it worked for me; After I installed these libraries(described in the post in the link above) through yarn i had to do also pod install and it worked.

I was using version v3.1.2 of react-native-push-notification which depends on outdated PushNotificationIOS, installing react-native-push-notification from main stream fixed my issue.

react-native-push-notification needs to create a new release to have this important commit: https://github.com/zo0r/react-native-push-notification/commit/5b02761d927cdacdc50fde41d58506f64d8154aa