Flutter Huawei Push Kit插件如何获取推送通知点击事件?
How to get push notification click event with Flutter Huawei Push Kit plugin?
我在 Flutter 应用程序中集成了华为推送套件 (https://pub.dev/packages/huawei_push),一切正常,除了当收到推送通知消息被点击以能够对其执行操作时,我无法获取事件。
是否可以通过此插件实现,或者我是否需要用本机 Android 代码编写此部分?
我也查看了他们的 flutter 插件,但找不到任何方法。我想您将不得不编写特定于平台的代码,为此您可以使用 PushKit 参考他们的原生演示 android 项目。
目前,您可以使用另一个侦听自定义意图的插件来实现此目的。 Uni_links 来自 pub.dev 的软件包易于使用。这是 uni_links 包的快速指南:
- 将 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 链接方法。我从 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 应用程序中集成了华为推送套件 (https://pub.dev/packages/huawei_push),一切正常,除了当收到推送通知消息被点击以能够对其执行操作时,我无法获取事件。
是否可以通过此插件实现,或者我是否需要用本机 Android 代码编写此部分?
我也查看了他们的 flutter 插件,但找不到任何方法。我想您将不得不编写特定于平台的代码,为此您可以使用 PushKit 参考他们的原生演示 android 项目。
目前,您可以使用另一个侦听自定义意图的插件来实现此目的。 Uni_links 来自 pub.dev 的软件包易于使用。这是 uni_links 包的快速指南:
- 将 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 链接方法。我从 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包含代码