Lädt...


🔧 Creating Dynamic Links and Deep Linking URLs in Node.js for React Native


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In the mobile app ecosystem, providing a seamless user experience is critical. One way to enhance this is by implementing deep links and dynamic links within your app. Dynamic links allow you to share content and ensure users have a smooth transition whether they have your app installed or not. In this blog, we'll explore how to create dynamic links and deep linking URLs using Node.js for a React Native application.
What Are Dynamic Links and Deep Links?
Deep Links: These are URLs that take users to a specific part of your app. When users click on a deep link, the app opens to the specific content the link is meant to display. If the app is not installed, the link typically opens the web version of the app or a fallback page.
Dynamic Links: These are deep links with added flexibility. They can change their behavior based on different conditions, such as whether the app is installed, the platform (iOS or Android), or even the context in which the link is opened (e.g., marketing campaigns).

Why Use Dynamic Links?
Cross-Platform Compatibility: Dynamic links work on multiple platforms, including iOS, Android, and web.
Deferred Deep Linking: If the app isn't installed, dynamic links can direct users to the app store and then to the specific content once the app is installed.
Enhanced User Experience: They provide a more personalized experience by directing users to the right content within your app.

Prerequisites
Before we dive in, make sure you have the following:
Node.js installed on your machine.
A React Native project setup.
Firebase account (optional, but recommended for generating dynamic links).

Step 1: Setting Up a Node.js Server

We'll start by setting up a Node.js server that will generate dynamic links. You can use Express.js to create a simple server.
First, create a new Node.js project:

mkdir dynamic-links-server
cd dynamic-links-server
npm init -y
npm install express shortid

Step 2: Generating Dynamic Links

const express = require("express");
const shortId = require("shortid")

const { appendSeoDetail } = require("./utils");
const app = express()

const PORT = process.env.PORT || 3000

app.use(express.json())
app.use("/.well-known", express.static('.well-known'))

const urls = {};

app.post("/create-url", (req, res) => {
  try {
    const { title, description, image, newsId } = req.body
    const previewUrl = `https://3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app/?newsId=${newsId}&title=${encodeURIComponent(title)}&description=${encodeURIComponent(description)}&image=${encodeURIComponent(image)}`
    const id = shortId.generate();
    urls[id] = previewUrl;
    res.send({ url: `https://3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app/${id}` })
  } catch (error) {
    res.send({ error: "Something went wrong" })
  }
})

app.get('/:id', (req, res) => {
  const id = req.params.id;
  const url = urls[id];

  if (url) {
    res.redirect(url);
  } else {
    res.sendStatus(404);
  }
});

app.get('*', (req, res,) => {
  const { title, description, image } = req.query
  // Define values
  const source = appendSeoDetail({ title, description, image })
  // Return the webpage
  return res.send(source);
});

app.listen(PORT, () => {
  console.log("App is running on ", PORT)
})

Step 3: Creating index html for the social share preview

<!DOCTYPE html>
<html lang="en">
 <head>
  <!-- HTML Meta Tags -->
  <title>{{title}}</title>
  <meta name="description" content="{{description}}" />

  <!-- Facebook Meta Tags -->
  <meta property="og:url" content="https://3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app/" />
  <meta property="og:type" content="website" />
  <meta property="og:title" content="{{title}}" />
  <meta property="og:description" content="{{description}}" />
  <meta property="og:image" content="{{image}}" />

  <!-- Twitter Meta Tags -->
  <meta name="twitter:card" content="summary_large_image" />
  <meta property="twitter:domain" content="3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app" />
  <meta property="twitter:url" content="https://3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app/" />
  <meta name="twitter:title" content="{{title}}" />
  <meta name="twitter:description" content="{{description}}" />
  <meta name="twitter:image" content="{{image}}" />

  <!-- Meta Tags Generated via https://www.opengraph.xyz -->

  <link
   rel="stylesheet"
   href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
   integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
   crossorigin="anonymous"
  />

  <script
   src="https://cdnjs.cloudflare.com/ajax/libs/Detect.js/2.2.2/detect.min.js"
   rossorigin="anonymous"
   referrerpolicy="no-referrer"
  ></script>
 </head>

 <body>
  <div class="container pt-3">
   <div class="text-center row justify-content-center">
    <div class="col-10">
     <a href="#">
      <img src="https://www.pouch.news/cropped-pulse%201.png" class="img-fluid" alt="" />
     </a>
    </div>
   </div>
   <div class="centered row mt-3 justify-content-center align-items-center">
    <div class="col-md-3 col-10 text-center">
     <p id="message"></p>
     <p id="redirectMessage text-center d-none d-md-block">Redirecting you to Example app.</p>
    </div>
   </div>
  </div>
 </body>

 <script>
  // Function to check if it's an Android device
  function isAndroid() {
   return navigator.userAgent.match(/Android/i)
  }

  // Function to check if it's an iOS device
  function isiOS() {
   return navigator.userAgent.match(/iPhone|iPad|iPod/i)
  }

  // Function to check if it's a desktop
  function isDesktop() {
   return window.innerWidth > 768 // Adjust the width threshold as needed
  }

  // Function to redirect to a website
  function redirectToWebsite() {
   window.location.href = "https://www.example.com/" // Replace with your desired URL
  }

  // Function to redirect to the Google Play Store
  function redirectToPlayStore() {
   window.location.href =
    "intent://details?id=com.example#Intent;scheme=market;package=com.example;end"
  }

  // Function to redirect to the App Store
  function redirectToAppStore() {
   window.location.href = "itms-apps://itunes.apple.com/app/appid2323" // Replace with your app's App Store URL
  }

  // Check the device type and display the appropriate message
  if (isDesktop()) {
   redirectToWebsite()
  } else if (isAndroid()) {
   document.getElementById("message").textContent =
    "We are redirecting you to the examplemobile app for Android."
   setTimeout(redirectToPlayStore, 3000) // Redirect after 3 seconds
  } else if (isiOS()) {
   document.getElementById("message").textContent = "We are redirecting you to the examplemobile app for iOS."
   setTimeout(redirectToAppStore, 3000) // Redirect after 3 seconds
  }
 </script>
</html>

Step 4: Creating utils for the appending dynamic seo title

const path = require("path")
const fs = require("fs");

function appendSeoDetail({ title, description, image }) {
  const _title = title ?? 'POUCH - Packaging News App';
  const _subtitle = description ?? 'Pouch is a news aggregator application for the Packaging industry that summarises news articles and stories sourced from various third-party links including industry journals, expert blogs, and innovative company releases';
  const _image = image ?? 'https://www.pouch.news/cropped-pulse%201.png';

  // Load HTML template
  const templatePath = path.join(__dirname, '../index.html');

  // Replace handles with content
  var source = fs.readFileSync(templatePath, { encoding: 'utf-8' })
    .replaceAll('{{title}}', _title)
    .replaceAll('{{description}}', _subtitle)
    .replaceAll('{{image}}', _image);

  return source
}

module.exports = { appendSeoDetail }

Step 5: Make a .well-know folder and insert files for

assetlinks.json and apple-app-site-association

{
  "applinks": {
    "details": [
      {
        "appIDs": [
          "EXDFF##.com.example"
        ],
        "paths": [
          "*"
        ]
      }
    ]
  },
  "webcredentials": {
    "apps": [
      "EXDFF##.com.example"
    ]
  }
}
[
  {
    "relation": [
      "delegate_permission/common.handle_all_urls"
    ],
    "target": {
      "namespace": "android_app",
      "package_name": "com.example",
      "sha256_cert_fingerprints": [
        "9B:83:32:66:75:91:03:3B:9C"
       ]
    }
  }
]

Step 7: Integrating Deep Links in React Native

const linking = {
  prefixes: ["example://", "https://3a09-2402-4cc0-2501-1f4-71ad-7a58-7a4c-895c.ngrok-free.app"],
  config,
  async getInitialURL() {
   const url = await Linking.getInitialURL()

   console.log(url, "Dynamic url")

   if (typeof url === "string") {
    return url
   }
js 
  },
 }

<NavigationContainer ref={navigationRef} linking={linking}>

</NavigationContainer>

Deep linking | React Navigation

This guide will describe how to configure your app to handle deep links on various platforms. To handle incoming links, you need to handle 2 scenarios:

favicon reactnavigation.org

Conclusion
By setting up dynamic links and deep linking in your React Native app with a Node.js backend, you can significantly enhance the user experience by providing seamless navigation and personalized content. Dynamic links offer a powerful way to ensure that users get to the right content, whether they have your app installed or not.
This setup is not just limited to Firebase. You can use other services like Branch.io or create a custom solution depending on your needs. The principles, however, remain the same: enabling users to move directly to the content they care about.
Happy coding!

...

🔧 Creating Dynamic Links and Deep Linking URLs in Node.js for React Native


📈 95.44 Punkte
🔧 Programmierung

🔧 Mastering Deep Linking and Universal Links in React Native: OpenGraph Share & Node.js Integration


📈 61.86 Punkte
🔧 Programmierung

🐧 Introduction to the ELF Format (Part VII): Dynamic Linking / Loading and the .dynamic section


📈 40.65 Punkte
🐧 Linux Tipps

🎥 No more broken links: Deep linking success in Flutter


📈 36.87 Punkte
🎥 Video | Youtube

🔧 Internal Linking yang Baik: Cara Meningkatkan SEO On-Page dengan Strategi Internal Linking yang Tepat


📈 36.64 Punkte
🔧 Programmierung

🔧 Deep Linking for Andriod and Apple in React.js


📈 35.37 Punkte
🔧 Programmierung

🔧 Creating Dynamic Forms with React, Typescript, React Hook Form and Zod


📈 35.31 Punkte
🔧 Programmierung

🎥 Deep dive into Flutter deep linking


📈 34.99 Punkte
🎥 Video | Youtube

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 34.99 Punkte
🔧 Programmierung

🔧 Deep Dive into apple-app-site-association file: Enhancing Deep Linking on iOS


📈 34.99 Punkte
🔧 Programmierung

🔧 React vs. React Native: Unveiling the Dynamic Duo of Modern Development


📈 32.88 Punkte
🔧 Programmierung

🔧 The State of React Native Tooling (React Native CLI - The Ultimate Guide)


📈 30.99 Punkte
🔧 Programmierung

🔧 React Native Networking – How To Perform API Requests In React Native using the FetchAPI


📈 30.99 Punkte
🔧 Programmierung

🔧 Building a Modern Document Website for React Native Library Like React Native ECharts


📈 30.99 Punkte
🔧 Programmierung

🔧 How to Implement Face Detection in React Native Using React Native Vision Camera


📈 30.99 Punkte
🔧 Programmierung

🔧 React Native Animation: Building Play/Pause Controls with Lottie in React Native


📈 30.99 Punkte
🔧 Programmierung

🔧 Creating and Linking VPC Endpoint of Type Interface using Terraform and AWS management console


📈 30.86 Punkte
🔧 Programmierung

🔧 Creating a Stylish Social links profile with Dynamic Button Animations


📈 29.76 Punkte
🔧 Programmierung

🔧 Handling React OTP Input Auth Web | React Native using react-otp-kit package


📈 29.6 Punkte
🔧 Programmierung

🔧 Handling React OTP Input Auth Web | React Native using react-otp-kit package


📈 29.6 Punkte
🔧 Programmierung

🎥 Creating a workload identity and linking it to a GitHub Actions deployment workflow


📈 29.19 Punkte
🎥 Video | Youtube

🔧 Creating an empty Angular project and linking it to an existing server on NestJS


📈 29.19 Punkte
🔧 Programmierung

📰 PSD2: Dynamic Linking – geht das bitte auch Compliance konform und bequem?


📈 28.65 Punkte
📰 IT Security Nachrichten

📰 PSD2’s dynamic linking in a mobile world


📈 28.65 Punkte
📰 IT Security Nachrichten

🐧 Dynamic linking: Over half of your libraries are used by fewer than 0.1% of your executables.


📈 28.65 Punkte
🐧 Linux Tipps

🐧 A discussion on static vs dynamic linking


📈 28.65 Punkte
🐧 Linux Tipps

🐧 [$] A look at dynamic linking


📈 28.65 Punkte
🐧 Linux Tipps

🔧 Passkeys & Dynamic Linking


📈 28.65 Punkte
🔧 Programmierung

🔧 Shared Library (Dynamic linking) - It's not about libs


📈 28.65 Punkte
🔧 Programmierung

🎥 Customizing Firebase Dynamic Links, Node.js on App Engine, & more! - TL;DR 116


📈 28.37 Punkte
🎥 Videos

matomo