evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 682 forks source link

Problems with expo. TypeError: undefined is not an object (evaluating 'RNFIRMessaging.requestPermissions') #949

Open konzertimpo opened 6 years ago

konzertimpo commented 6 years ago

I have a problem starting the app in expo (Android & iOS).

Every time i try to to call a Method from FCM, i get an error like the following:

TypeError: undefined is not an object (evaluating 'RNFIRMessaging.requestPermissions')

This error is located at:
    in PushNotification (at App.js:44)
    in RCTView (at View.js:60)
    in View (at App.js:42)
    in App (at registerRootComponent.js:35)
    in RootErrorBoundary (at registerRootComponent.js:34)
    in ExpoRootComponent (at renderApplication.js:33)
    in RCTView (at View.js:60)
    in View (at AppContainer.js:102)
    in RCTView (at View.js:60)
    in View (at AppContainer.js:122)
    in AppContainer (at renderApplication.js:32)

Here is the code I use:

import React, { Component } from "react";

import FCM, { FCMEvent } from "react-native-fcm";

export default class PushNotification extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {

    // this method generate fcm token.  
    FCM.requestPermissions();
    FCM.getFCMToken().then(token => {
      console.info("TOKEN (getFCMToken)", token);
    });

    FCM.subscribeToTopic('all');

    // This method get all notification from server side.
    FCM.getInitialNotification().then(notif => {
      console.info("INITIAL NOTIFICATION", notif)
    });

    ...

When I compile to APK everything is working fine. What am I doing wrong? Is the package not compatible with expo?

I know similar problems where already discussed for Android and iOS builds. But i found nothing related to expo...

evollu commented 6 years ago

I'm not sure. you can remove import from "react-native-fcm";

This kind of error shows us mostly because not integrated correctly. for example, missing the change in MainActivity.java

konzertimpo commented 6 years ago

import from "react-native-fcm" was a copy&past mistake and is not in the code ;)

The MainActivity.java looks like this:

package com.mytestapp;

import com.facebook.react.ReactActivity;
import android.content.Intent;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "MyTestApp";
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }   
}

As I mentioned before, everything works fine with react-native run-android. But if I try to start it with npm start and expo, the error occurs.