react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

evaluating RNPushNotification.getInitialNotification is not an object

Open hanaechahid opened this issue 6 years ago • 29 comments

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?!

hanaechahid avatar Mar 06 '20 16:03 hanaechahid

Wait for a fix or just downgrade. I have

"react-native-push-notification": "^3.1.1",

and this works

GeldgeilerGnom avatar Mar 09 '20 11:03 GeldgeilerGnom

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

alpamys-qanybet avatar Mar 09 '20 22:03 alpamys-qanybet

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)

hanaechahid avatar Mar 10 '20 10:03 hanaechahid

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.
 }

alpamys-qanybet avatar Mar 12 '20 14:03 alpamys-qanybet

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 avatar May 11 '20 20:05 Shamzic

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

val2207 avatar May 30 '20 13:05 val2207

Me too :((

Kasmadei avatar Jun 04 '20 15:06 Kasmadei

@Kasmadei Did you find the solution?

AndersonSchmidt avatar Jun 10 '20 13:06 AndersonSchmidt

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'

acSpock avatar Aug 22 '20 19:08 acSpock

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

RNCLI

michaelVictoriaDev avatar Oct 16 '20 06:10 michaelVictoriaDev

I solved my issue

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

michaelVictoriaDev avatar Oct 16 '20 09:10 michaelVictoriaDev

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

RNCrazy99 avatar Nov 03 '20 19:11 RNCrazy99

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

anonewb avatar Nov 05 '20 04:11 anonewb

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

jan-ambroz avatar Dec 24 '20 14:12 jan-ambroz

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

Dallas62 avatar Dec 24 '20 15:12 Dallas62

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 !

tarunkishore2303 avatar Jan 02 '21 17:01 tarunkishore2303

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.

Dallas62 avatar Jan 02 '21 17:01 Dallas62

Is that necessary for android ?

tarunkishore2303 avatar Jan 02 '21 17:01 tarunkishore2303

Did you tried: https://github.com/zo0r/react-native-push-notification/issues/1335#issuecomment-678685014 ?

Dallas62 avatar Jan 02 '21 17:01 Dallas62

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 avatar Jan 02 '21 18:01 tarunkishore2303