flutter - 如何使用Flutter华为Push Kit插件获取推送通知点击事件?

标签 flutter huawei-mobile-services huawei-developers

我正在 Flutter 应用程序中集成华为推送套件 ( https://pub.dev/packages/huawei_push ),一切正常,只是当单击收到的推送通知消息并对其进行操作时无法获取事件。

这可以通过这个插件实现吗?还是我需要用原生 Android 代码编写这一部分?

最佳答案

目前,您可以使用另一个监听自定义意图的插件来实现此目的。 Uni_links pub.dev 的包很容易使用。这是 uni_links 包的快速指南:

  1. uni_links 添加到您的 pubspec.yaml 文件中:
dependencies:
  flutter:
    sdk: flutter
  huawei_push: 4.0.4+300
  uni_links: 0.4.0
  • AndroidManifest.xml 文件中定义 Intent 过滤器:
  • <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
    
  • 在 initState() 中调用 uni links 方法来监听自定义意图。我从 Push Kit 控制台发送的通知的自定义意图如下所示:
  • 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());
      });
    }
    

    欲了解更多信息,请访问:Deep Linking on Flutter using Huawei Push Kit’s Custom Intents accompanying github repository文章包含代码。

    关于flutter - 如何使用Flutter华为Push Kit插件获取推送通知点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63451613/

    相关文章:

    firebase - 检查Firebase文档中的特定字段,如果找到字段,则返回文档ID。

    android - 无法在 Android Studio 中为 Flutter 项目添加依赖项

    huawei-mobile-services - appgallery connect.上传包。出现未知错误。请稍后再试

    android - sendMessage 无法正常工作(可穿戴)

    huawei-mobile-services - 皇家海军 : Not able to enable account kit from manage API in AGC Console

    huawei-mobile-services - 未解析引用: xms.华为Xms适配层不工作

    ios - Flutter 在构建 ios 时显示错误?

    huawei-mobile-services - HMS Ads Kit - 无法解析 com.huawei.hms :ads-lite:13. 4.29.303

    flutter - HMS Core Playstore 版本无法使用华为帐号登录

    flutter - 在flutter插件image_picker示例中从图库中选择图像时内存增加