Deep Linking in iOS and Android In-App Notifications

Deep links create an experience that links users directly to the content they desire. Creating a flexible deep link architecture in your application and using tools like Mixpanel’s People Notifications allows your mobile team to drive user engagement to precise locations quickly and effectively.

In today’s mobile ecosystem, moving between applications or within applications can be friction-filled and lead to low conversions. For example, when sending a notification to have your users try a new feature, a deep link can point users precisely to the relevant view. 

Deep link in push notifications using this guide.

The iOS and Android examples are available in sample apps here: iOSAndroid.

iOS (Objective-C)

In iOS you need to follow a three-step process to set up deep linking. The first two steps will require changes to your Xcode build. Examples will be in Objective-C.

  1. Register a custom URL within Xcode. Having a custom URL scheme gives your application a name iOS can recognize. When presented with your unique URL, iOS will open your application.
  2. Update ourAppDelegate.m to listen for additional parameters on the URL. While step 1 helps iOS know to open your application, step 2’s AppDelegate alteration provides direction as to what screen the user should see on app open. 
  3. Deliver a deep link through Mixpanel in-app notifications. This is one effective way to make your notifications harmonious.

Step 1 – Register a custom URL

In your Xcode project, we will need to register a custom URL identifier. Access your info.plist file within your Supporting Files. Add a new row for URL Types. Expand Item 0 and create a URL identifier. Best practices assert naming your domain in reverse: com.mixpanel.Deeplinkapp. You will not need to reference this URL identifier later.

Under Item 0 you can add a new row to create a URL scheme. This gives your application the specific naming convention. Now, when iOS is presented with your registered URL scheme, it knows to launch your application.

Under the URL Type, there is a new row categorized as URL Schemes. Its string is set to "mixpanel."

In your iOS simulator, test the URL scheme by opening Safari and entering in your scheme. Your application should launch:

mixpanel://

Step 2 – Listen for custom URLs

Your URL scheme will open your application to its home screen. Next, you'll want to direct a user to a specific view other than the home screen.

The format of these URLs are scheme, host, and path: scheme://[host]/[path]. The scheme launches the application, and the host and the path move the user to a separate screen within the application.

Update your AppDelegate’s default behavior to listen for a host and a path. Add the following code to your AppDelegate. This allows for additional parameters to be passed into a custom URL:

-(BOOL)application:(UIApplication *)application
 openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
 annotation:(id)annotation

Now we need to update this listener to look for a specific host and path. Below shows an if statement that directs a user to the correct view controller given a match. If mixpanel://deeplink/page1 is clicked, it will direct to thePage1ViewController, a separate screen from the home view.

This code snippet alters the previous function:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if([[url host] isEqualToString:@"deeplink"]) {
        if([[url path] isEqualToString:@"/page1"]) {
            self.window.rootViewController = [[Page1ViewController alloc] init];
        } else if ([[url path] isEqualToString:@"/page2"]) {
            self.window.rootViewController = [[Page2ViewController alloc] init];
        } else {
            return NO;
        }
    } else {
        return NO;
    }
    return YES;
}

Step 3 – Deliver deep link through Mixpanel

Finally, now that your application is set up to listen for specific information, let’s send that information over through Mixpanel!

In the Notifications tab, there is a field below touch actions. When your user taps your notification's call to action, you can order your application to ope n a link in or outside of your app. This field can be a weblink such as http://mixpanel.com or a deep link like fb://profile/33138223345. Deep links allow apps to open specific pages in other apps such as Maps, Facebook, and the App Store using a customer protocol described on Wikipedia.

Mixpanel link will be: mixpanel://deeplink/page1. Any user who has our application on their phone will be directed to ‘page1’ attached to the Page1ViewController.

This is page1, what the user will see after touching the deep linking action.


Android Java

In Android, the deep link setup requires creating an intent filter to register a URL scheme. Upon the creation of an intent filter, we’ll utilize the data provided by the intent to determine which screen to render.

Registering URI with Action, data, category

Three elements within an intent are action, data, and category.

Action - describes what the user is trying to do.
Data - describes the specific URI that was clicked on.
Category - allows an intent filter to be accessible from either a web browser or an implicit intent by including BROWSABLE and DEFAULT. For more, feel free to read the Android documentation under ‘Add Intent Filters For Your Deep Links’.

You will now need to wrap these elements in an intent filter within your Android Manifest. Below, you’ll see Mixpanel utilizes the data element to register a URL scheme mpdeeplink:// and added a host called ‘newcontent’. Now, when given mpdeeplink://newcontentpage, your Android Manifest is prepared to execute an action.

Handling the Intent

Android has instantiated an activity in respect to the intent filter you have set up. Now, you can set up an intent handler to appropriately direct the user to the screen associated with the provided scheme and host. The intent data is retrieved within onCreate, and you can use ‘Uri data’ to create an activity.

The below activity shows an alert and tracks an event:

public class NewContentActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);\
        if (getIntent().getAction() == Intent.ACTION_VIEW) {
            Uri data = getIntent().getData();
            if (data != null) {
                // show an alert with the "custom" param
                new AlertDialog.Builder(this)
                        .setTitle("Welcome to New Content!")
                        .setMessage("Found custom param: " +data.getQueryParameter("custom"))
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        }
        // send a Mixpanel event
        MixpanelAPI mMixpanel = MixpanelAPI.getInstance(this, MainActivity.MIXPANEL_API_TOKEN);
        mMixpanel.track("in app activity event", null);
        mMixpanel.flush();
    }
}

Deliver Through Mixpanel In-App Messages

Once you’ve created an activity for your users, test it with the Mixpanel web app. Utilize the ‘Touch Actions’ field in the in-app message creation builder to fill in your custom URL.

Fill your custom URL into the Touch Actions field to enable deep linking.

Is this article helpful?
1 out of 8 found this helpful

Comments

0 comments

Please sign in to leave a comment.