C-Fox
C-Fox

Reputation: 33

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?

Upvotes: 3

Views: 1511

Answers (3)

Ahmad Izaz
Ahmad Izaz

Reputation: 95

its an old thread but for the update, i guess they have this callback now when you click on notification when app is in background or killed.

Push.onNotificationOpenedApp.listen(_onNotificationOpenedApp,
    onError: _onNotificationOpenedAppError,
    onDone: _onNotificationOpenedAppDone);

Keep in mind, you need to build the notification UI to show in notification tray as huawei does not do that and when your app is in background or killed you will receive the data message in this callback

 Push.registerBackgroundMessageHandler(backgroundMessageCallback);
static void backgroundMessageCallback(RemoteMessage remoteMessage) async {
print(
    "MessagingRepositoryHuawei - backgroundMessageCallback: $remoteMessage");
//String data = remoteMessage.data;

Push.localNotification({
  HMSLocalNotificationAttr.TITLE: '[Headless] DataMessage Received',
  HMSLocalNotificationAttr.MESSAGE: remoteMessage.data
});

}

Upvotes: 0

zhangxaochen
zhangxaochen

Reputation: 33997

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.

Upvotes: 2

Anas Iqbal
Anas Iqbal

Reputation: 206

I also looked into their flutter plugin and couldn't any method for that. I guess you will have to write platform specific code and for that you can refer to their demo native android project with PushKit.

https://github.com/HMS-Core/hms-push-clientdemo-android

Upvotes: 0

Related Questions