Skip to content

Instantly share code, notes, and snippets.

@naishe
Created December 6, 2020 10:06
Show Gist options
  • Save naishe/1eba531e9cbea51947f60b0df3ce61c1 to your computer and use it in GitHub Desktop.
Save naishe/1eba531e9cbea51947f60b0df3ce61c1 to your computer and use it in GitHub Desktop.
Deep Linking Push Notifications with React Navigation #0
// Simple scenario without nested routes, without path parameters
function App() {
const navigation = useNavigation();
const [loading, setLoading] = useState(true);
const [initialRoute, setInitialRoute] = useState('Home');
useEffect(() => {
// 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(remoteMessage.data.type);
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
}
setLoading(false);
});
}, []);
if (loading) {
return null;
}
return (
<NavigationContainer>
<Stack.Navigator initialRouteName={initialRoute}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment