Skip to main content

How to Enable Post Sharing and Deep Links Parsing for Posts?

Introduction

Enabling post sharing is a crucial feature that allows content to be shared outside the community, thereby increasing engagement and visibility. In this guide, you'll learn how to enable sharing for posts using the LikeMinds Feed Flutter SDK. This involves setting up the feed with the correct domain, triggering the share functionality, and implementing link parsing to handle shared links properly.

Prerequisites

Before you begin, ensure the following:

  • LikeMinds Feed Flutter SDK: The SDK must be properly installed and initialized in your Flutter project. Refer to the installation guide if needed.
  • Basic Understanding of Flutter: Familiarity with Flutter widgets and navigation.
  • Link Handling Knowledge: Basic understanding of handling deep links on both Android and iOS platforms.

Steps

Step 1: Initialize Feed with Domain Key

Pass the domain key While calling initialize method of LMFeedCore class inside your main() method.

void main() async {
// initialize bindings
WidgetsFlutterBinding.ensureInitialized();
...
// initialize LikeMinds Feed SDK
await LMFeedCore.instance.initialize(
domain: "YOUR_DOMAIN_HERE",
);
...
}

Step 2: Trigger Share Post Flow

We directly open the default share options, based on the platform (iOS or Android), on the click of our default share button inside a post.

Our format of shared links for a post is - https://YOUR-DOMAIN/post?post_id=POST-ID

Alternatively, you can use the snippet below to call the function used in that button to trigger the share post flow on your end, if you are extending the existing Share button with customizations.

LMFeedDeepLinkHandler().sharePost("POST-ID"); // Pass the specific post ID here

Implement a link parsing function within your app to handle links clicked when posts are shared. This ensures that your app can capture and process these links correctly.

Official Documentation

Common Steps

  1. Add Entries: Add appropriate entries in AndroidManifest.xml for Android and info.plist for iOS.
  2. Server-Side Verification: Build a server-side link verification system for iOS.
  3. Intercept Links: Use a package to intercept and handle the link when the app opens.

Once you have verified that your app is opening with the link, proceed to the next step.

Verify if the incoming link is from LikeMinds and contains the necessary post information.

The format of shared links for a post is: https://YOUR-DOMAIN/post?post_id=POST-ID

Use the following Dart snippet to check and process the link:

// Get the initial link with which the app was opened
final initialLink = "YOUR-PARSED-LINK";

// Parse it using Uri.parse()
final uri = Uri.parse(initialLink);

// Check if URI is absolute
if (uri.isAbsolute) {
// Check for LM post detail link schema
// It will contain a /post segment
// and a post id along with it
if (uri.path == '/post') {
List<String> secondPath = initialLink.split('post_id=');
if (secondPath.length > 1 && secondPath[1] != null) {
// post_id is the secondSegment here
String postId = secondPath[1];

// Call showFeedWithoutApiKey method
// This will call the onboarding APIs, required for
// the proper functioning of the Feed SDK
LMResponse response =
await LMFeedCore.instance.showFeedWithoutApiKey();

if (response.success) {
navigateToPostDetailScreen(postId); // Implemented below
} else {
// handle error state using
// response.errorMessage
}
}
}
}

Step 5: Navigate to Post Detail Screen

After parsing the link and obtaining the postId, navigate to the LMPostDetailScreen using the following snippet:

void navigateToPostDetailScreen(String postId) {
// Build a material route using your preferred navigation method
MaterialPageRoute route = MaterialPageRoute(
builder: (context) => LMFeedPostDetailScreen(
postId: postId, // Required variable
),
);
// Navigate to post details screen
Navigator.of(context).push(route);
}

This completes the setup for handling shared post links and navigating to the appropriate screens within your app.