Deep Linking on Flutter using Huawei Push Kit’s Custom Intents

Open any page or show related content on your app by the tap of a push notification

Ali Türkay Avci
Huawei Developers
7 min readAug 7, 2020

--

In this article I will show the basics of deep linking for Flutter, using Huawei Push Kit Plugin along with uni_links package.

Here are the links for those packages:

Important Update: Huawei Push Kit Plugin for Flutter supports Deep Linking with Custom Intents out of the box after the 5.0.2+300 release. This means you don’t have to use the uni_links package anymore for the versions 5.0.2+300 or after.

Check the development guide here for the deep linking steps with the Push Kit Flutter Plugin. The usage is very similar to uni_links package so you can apply the same steps written in this article with minor changes.

Deep Linking

The most basic definition for a deep link is: “A link that sends users to related content on an application”.

Okay but why is this important ?

For improving the User Experience (UX) of course. By utilizing custom uniform resource identifiers (URIs), developers can create funnels in their apps for landing users to the specific content and make the user experience better.

We can validate this with an example: An e-commerce application on your phone has sent you a notification that there will be a discount on stickers. Would you prefer going to the stickers page by tapping the notification or rather navigate by yourself through the enourmous menus like Home > Products > Handmade Products > Stationery & Party Supplies > Stationery Stickers. I am assuming you would choose the first aproach.

Huawei Push Kit allows developers to send push notifications that can include custom intents or actions. This is well suited for our case. So let’s get started for the sake of our users’ experience.

Prerequisites

Before we begin, there are prerequisites that need to be completed.

  • Huawei Developer Account: you must have this account to use the Push Kit Service. Click here to sign up if you don’t have an account already.
  • HMS Core SDK setup: For using the Push Kit we have to make some configurations on Huawei Developer Console and in our application. Refer to this medium post for installation and if you have any trouble doing so you can also check this post for a more in-depth setup.

The Project

The project will be a very simple app that will display information about Huawei Mobile Services. Here is the project’s Github link if you want to follow from there.

Project Setup

As I’ve mentioned before we will use uni_links and Huawei Push Kit plugins in our project. We will also add flutter_webview_plugin into the mix for displaying the website content of the service. So let’s start by adding these to our pubspec.yaml file

To listen for intents, uni_links package needs a configuration in the AndroidManifest.xml file. Add an intent filter inside <activity> tag like below.

Now that we are done with the installation let’s get started with coding. The project is very simple, you can check the file hierarchy below.

Project’s file hierarchy

We have two app pages. Home page will show various Huawei Mobile Services and the content page will display the service information inside a webview. We will navigate to this page if a notification with custom intent is tapped.

Main.dart

This main.dart file is almost identical with what you get for default except for the named route generation. Here, I have defined the onGenerateRoute and initialRoute properties of the MaterialApp and I have passed Router’s generateRoute method. Let’s look at the router.dart file to see what’s going on.

Router.dart

I used named routes because they contain less boilerplate and easier to use with custom intents. For using the named routes in Flutter we should set the names for our routes and return the corresponding MaterialPageRoute based on the name. Here, I am also getting the arguments needed for the content page and passing to its widget. (We put these arguments when we call the navigator)

Hms.dart

This class’ mere purpose is to hold the data for the names and URLs for the HMS services that will be displayed on our home page.

I’ve deleted some of the definitions to not bother you with details. Check the github repo for the full code.

Home_page.dart

This is the widget that the most important functions occur so I will split into parts and get into some details for a better explanation.

You can refer to this part for creating your own custom intent navigations for the purpose of deep linking.

Obtaining a push token

For sending push notifications we need to obtain a push token for our device.

Under the state of the widget, define an EventChannel that will listen for the push token and a string variable to hold the token.

Initialize functions below for obtaining the push token.

Call the initPlatformState function on the widget’s initState method. You should now see your token printed on the debug console.

Obtaining a push token is the most crucial part when using the push kit. If you run into any errors while obtaining the token here is checklist that could help:

  1. Check your SHA-256 signature and package name on the Huawei Developer Console
  2. Make sure the Push Kit is enabled on the console (Manage APIs tab)
  3. Whenever you change something on the console, download the agconnect-services.json file again.

Deep linking

We will need two functions to listen for the custom intents: One is for when our app is on foreground (active) and the other one is for when the app is not active and it is opened by an intent.

Call these functions on the widgets initState

Our custom intent inside the push notification looks like this: app:///ContentPage?name=Push Kit&url=https://developer.huawei.com/consumer/en/hms/huawei-pushkit

By adding query params, we can utilize the Uri class’ queryParameters method to easily obtain the values we need and not worry about string parsing.

Now for the final part of home_page.dart here is the UI code below.

Content_page.dart

The last file is content_page.dart. This widget is very simple since it’s only purpose is to display the related service content inside a webview.

You can see the app on action below.

Sending Push Notifications with Custom Intent

Now for the last part let’s head over to Huawei Developer Console and create a push notification that include a custom intent. Enter the details like in the image below and press “Test Effect” button or submit your push notification from top right corner.

You can find the custom intent uri entered here on the deep linking section of this article

If you press “Test Effect” button console will prompt you to enter the token you obtained earlier.

Enter the push token obtained earlier in the app

Deep Linking while app is on foreground.

Deep Linking when app is not active.

Deep linking is working as expected. Kudos to you if you got this far!

Conclusion

Push notifications and deep linking is a must for a mobile application nowadays since their use can boost up user retention and experience if used properly. Huawei Push Kit’s notifications include custom intent and custom action features for the use of deep linking but they aren’t limited with these ones only. If you want to check all the features click here.

I hope this tutorial was helpful for you. If you have any questions regarding this article feel free to ask them on the comment section.

You can also check our other articles about using Huawei Mobile Services on Flutter below.

References

--

--