Search code examples
flutterhuawei-mobile-serviceshuawei-developers

How to get push notification click event with Flutter Huawei Push Kit plugin?


I am integrating Huawei Push Kit (https://pub.dev/packages/huawei_push) in Flutter application and everything works fine except I am unable to get the event when received push notification message is clicked to be able to act on it.

Is this possible to achieve via this plugin or do I need to write this part in native Android code?


Solution

  • Currently, you can achieve this with another plugin that listens for the custom intents. Uni_links package from pub.dev is easy to use. Here is a quick guide to uni_links package:

    1. Add uni_links to your pubspec.yaml file:
    dependencies:
      flutter:
        sdk: flutter
      huawei_push: 4.0.4+300
      uni_links: 0.4.0
    
    1. Define an intent filter on your AndroidManifest.xml file:
    <application
      <!-- . . . Other Configurations . . . -->
        <activity/>
          <!-- . . . Other Configurations . . . -->
            <!-- Add the intent filter below.(inside the application and activity tags) -->
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="app"/>
                </intent-filter>
            </activity>
    </application
    
    1. Call the uni links methods in your initState() that will listen for the custom intents. The notification's custom intent I've sent from the Push Kit Console looks like this:
    app:///ContentPage?name=Push Kit&url=https://developer.huawei.com/consumer/en/hms/huawei-pushkit
    
    // Get the initial intent that opens the app
    Future<void> initInitialLinks() async {
      // Platform messages may fail, so we use a try/catch PlatformException.
      try {
        String initialLink = await getInitialLink();
        if (initialLink != null) {
          var uri = Uri.dataFromString(initialLink);
          String page = uri.path.split('://')[1];
          String serviceName = uri.queryParameters['name'];
          String serviceUrl = uri.queryParameters['url'];
          try {
            WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
              Navigator.of(context).pushNamed(
                page,
                arguments: ContentPageArguments(serviceName, serviceUrl),
              ); // Navigate to the page from the intent
            });
          } catch (e) {
            Push.showToast(e);
          }
        }
      } on PlatformException {
       print('Error: Platform Exception');
      }
    }
    
    // Get intents as a stream
    Future<Null> initLinkStream() async {
      if (!mounted) return;
      _sub = getLinksStream().listen((String link) {
        var uri = Uri.dataFromString(link);
        String page = uri.path.split('://')[1];
        // Parse the string ...
        Navigator.of(context).pushNamed(page); // Navigate to a page from the intent
      }, onError: (err) {
        print("Error while listening for the link stream: " + err.toString());
      });
    }
    

    For more information, visit: Deep Linking on Flutter using Huawei Push Kit’s Custom Intents The accompanying github repository of the article includes the codes.