Skip to content

Instantly share code, notes, and snippets.

@naishe
Created December 6, 2020 09:51
Show Gist options
  • Save naishe/fbc0ca0a3fa81da1872c96902d4c3140 to your computer and use it in GitHub Desktop.
Save naishe/fbc0ca0a3fa81da1872c96902d4c3140 to your computer and use it in GitHub Desktop.
Deep Linking Push Notifications with React Navigation #6
// App.tsx
// only new changes shown, refer: https://gist.github.com/naishe/46dfb1e23612398e4bd525f03c001dd9
const linking: LinkingOptions = {
prefixes: ['myapp://', 'https://app.myapp.com'],
config: deepLinksConf,
async getInitialURL() { /* redacted */ },
subscribe(listener) {
const onReceiveURL = ({url}: {url: string}) => listener(url);
// Listen to incoming links from deep linking
Linking.addEventListener('url', onReceiveURL);
// Listen to firebase push notifications
const unsubscribeNotification = messaging().onNotificationOpenedApp(
(message) => {
const url = message?.data?.link;
if (url) {
// Any custom logic to check whether the URL needs to be handled
// Call the listener to let React Navigation handle the URL
listener(url);
}
},
);
return () => {
// Clean up the event listeners
Linking.removeEventListener('url', onReceiveURL);
unsubscribeNotification();
};
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment