Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 30345

how to navigate to specific screen after pressing rnfirebase notification

$
0
0

i'm using react native firebase and i'm getting notification whenever is needed, and these notifications have some data to navigate to specific screen.i used the firebase documentation to implement the functionality but it's not working as it's supposed to

Here is the document i've used Firebase & React-Navigationand my code looks something like this :

const Stack = createStackNavigator();const Router = () => {    const navigation = useNavigation();    const [loading, setLoading] = useState(true);    const [initialRoute, setInitialRoute] = useState('Splash');useEffect(() => {    //fcm    registerAppWithFCM();    // checkRNFBPermission();    const unsubscribe = messaging().onMessage(async remoteMessage => {        console.log('remote DATAAAAAAAAAAAAAAAAAAAAAAAA : ',remoteMessage.data);        // switch (remoteMessage.data.screen) {        //     case 'answer':{        //         console.log('inside switch condition 1 !!!!!!!!!!!!!');        //         useNavigation().navigate('Profile');        //         break;        //     }        //     case 'AnswerQuestion':{        //         console.log('inside switch condition 2 !!!!!!!!!!!!!');        //         useNavigation().navigate('Profile');        //         break;        //     }        //     default:        //         break;        // }        // Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));        // const owner = JSON.parse(remoteMessage.data.owner);        // const user = JSON.parse(remoteMessage.data.user);        // const picture = JSON.parse(remoteMessage.data.picture);    });    // Assume a message-notification contains a "type" property in the data payload of the screen to open   messaging().onNotificationOpenedApp(remoteMessage => {      console.log('Notification caused app to open from background state:',        remoteMessage.notification,      );      navigation.navigate('Profile');    });    //  Check whether an initial notification is available    messaging()    .getInitialNotification()    .then(remoteMessage => {      if (remoteMessage) {        console.log('Notification caused app to open from quit state:',          remoteMessage.data, //notification        );      }      setLoading(false);    });    messaging().setBackgroundMessageHandler(async remoteMessage => {        console.log('Message handled in the background!', remoteMessage);    });    return unsubscribe;    //fcm}, []);//fcmcheckRNFBPermission = async() => {    const enabled = await messaging().hasPermission();    if(enabled){        messaging()        .getToken()        .then(token => {            // console.log('deviceeeee fcm token ------> ', token);        });        }else{        requestUserPermission();    }}registerAppWithFCM = async() => {    await messaging().registerDeviceForRemoteMessages();}requestUserPermission = async() =>  {    const settings = await messaging().requestPermission();    if (settings) {        console.log('Permission settings:', settings);    }}//fcmrenderLoading = () => (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center'  }}><Text>Domanda</Text><ActivityIndicator size='large' color={colors.darkerTeal} /></View>);//firebaseif (loading) {    return null;}//firebasereturn(<Provider store={store}><PersistGate persistor={persistor} loading={this.renderLoading()}><Root><NavigationContainer><Stack.Navigator initialRouteName={initialRoute} headerMode="none"><Stack.Screen name="Splash" component={Splash} /><Stack.Screen name="Login" component={Login} /><Stack.Screen name="Main" component={Main} /><Stack.Screen name="AppIntro" component={AppIntro} /><Stack.Screen name="Tags" component={Tags} /><Stack.Screen name="Answers" component={Answers} /><Stack.Screen name="Profile" component={Profile} /><Stack.Screen name="EditInfo" component={EditInfo} /><Stack.Screen name="ChangePassword" component={ChangePassword} /><Stack.Screen name="AnswerQuestion" component={AnswerQuestion} /><Stack.Screen name="ContactUs" component={ContactUs} /></Stack.Navigator></NavigationContainer></Root></PersistGate></Provider>)};export default Router;

but when i add usenavigation and i want to use it it throws this error:Error: We couldn't find a navigation object. Is your component inside a screen in a navigator?

enter image description here

i can not use navigation.navigate('Profile'); to navigate to a specific screen.


Viewing all articles
Browse latest Browse all 30345

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>