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

856

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.

Share:
856
C-Fox
Author by

C-Fox

Updated on December 23, 2022

Comments

  • C-Fox
    C-Fox over 1 year

    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?

  • C-Fox
    C-Fox over 3 years
    Thanks, I implemented it like that, but this is actually a workaround. I was hoping there was more direct approach to get the content of the push notification message when clicked or immediately if the app is in foreground as you can do with firebase_messaging. But ok, this solution is good enough :)
  • shirley
    shirley over 3 years
    Yes, currently deep linking for Flutter could be achieved by using Huawei Push Kit Plugin along with uni_links package.