Created
December 6, 2020 10:06
-
-
Save naishe/1eba531e9cbea51947f60b0df3ce61c1 to your computer and use it in GitHub Desktop.
Deep Linking Push Notifications with React Navigation #0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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