Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
getInitialURL
and subscribe
options to linking config
For apps with push notifications linking to screens inside the app, currently we need to handle them separately (e.g. [instructions for firebase](https://rnfirebase.io/messaging/notifications#handling-interaction), [instructions for expo notifications](https://docs.expo.io/push-notifications/receiving-notifications/)). But if we add a link in the notification to use for deep linking, we can instead reuse the same deep linking logic instead. This commit adds the `getInitialURL` and `subscribe` options which internally used `Linking` API to allow more advanced implementations by combining it with other sources such as push notifications. Example usage with Firebase notifications could look like this: ```js const linking = { prefixes: ['myapp://', 'https://myapp.com'], async getInitialURL() { // Check if app was opened from a deep link const url = await Linking.getInitialURL(); if (url != null) { return url; } // Check if there is an initial firebase notification const message = await messaging().getInitialNotification(); // Get the `url` property from the notification which corresponds to a screen // This property needs to be set on the notification payload when sending it return message?.notification.url; }, 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.notification.url; if (url) { // If the notification has a `url` property, use it for linking listener(url); } } ); return () => { // Clean up the event listeners Linking.removeEventListener('url', onReceiveURL); unsubscribeNotification(); }; }, config, }; ```
- Loading branch information