New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
route when push notification is opened #742
Comments
The solution may involve this https://reactnavigation.org/docs/navigators/#Calling-Navigate-on-Top-Level-Component |
I use redux, and have a handler for the push-notification which triggers a navigation-action, works pretty smooth - although the correct view is pushed on the stack when the app is opened (would be neat to have it rendered initially) but works brilliantly for my use-case. |
@raubas Where is the handler implemented? On your top level component? If so, I am confused how to use navigationActions from the top level component since I do not know how to dispatch them without access to a screen's navigation prop. Am I being stupid lol? |
@ddeanto I have it set up in my root views component did mount where I register for notifications and Deep-links (using branch.io) Basically I have a onNotification function which extracts all the params needed from the notification, and call: this.props.dispatch( handleDeepLink(pushparams) ) Then I have som actions defined for handling deep links and navigation,
|
@raubas If you use navigate or dispatch reset action. it might redirect to the new screen, not specified route we wanted. i've tried with this code:
My schema: StackNavigator
Now i want to navigate to Main (TabNavigator) and change to Tab2 |
i fixed with this
more info: |
This has been a simple relatively bulletproof way of handling push notifications that work in a variety of circumstances with "react-navigation" and "react-native-push-notification'" ex:
This also navigates to a relatively nested route (a stack navigator in a switch navigator and resets the navigation hierarchy correctly under the above circumstances)
|
Using @johnculviner response i have added following code into my App.js for handling navigations from push notification. Also for NavigationService file please refer here. import React, { Component } from "react";
import Nav from "./src/routes";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import store, { persistor } from "./src/redux";
import firebase from "react-native-firebase";
import NavigationService from './src/utils/helpers/NavigationService';
const Deferred = () => {
var d = {};
d.promise = new Promise(function(resolve, reject) {
d.resolve = resolve;
d.reject = reject;
});
return d;
};
const navigationDeferred = new Deferred()
class App extends Component {
componentDidMount() {
this.createNotificationListeners();
}
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase
.notifications()
.onNotification(notification => {
NavigationService.navigate(notification._data.url);
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(async (notificationOpen) => {
NavigationService.navigate(notificationOpen.notification._data.url);
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
navigationDeferred.promise.then(() => {
NavigationService.navigate(notificationOpen.notification._data.url);
});
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage(message => {
//process data message
console.log(JSON.stringify(message));
});
}
render() {
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Nav ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
navigationDeferred.resolve(navigatorRef);
}} />
</PersistGate>
</Provider>
);
}
}
export default App; |
@zashishz can you please attatch your all code releted other files for better understanding. thank you :) |
I want to route the user to a specific screen when they open a push notification. I don't understand how to do this because it seems I can't pass the navigation prop to myhighest order component.
For example, I want to do something like this: http://stackoverflow.com/questions/42359027/react-native-render-a-page-on-push-notification
The text was updated successfully, but these errors were encountered: