Flutter应用消息推送插件agconnect_appmessaging的使用
Flutter应用消息推送插件agconnect_appmessaging的使用
简介
您可以使用AppGallery Connect的应用消息功能向正在使用您的应用的目标用户发送相关消息,鼓励他们使用关键应用功能,或者发送吸引人的促销活动以增强用户忠诚度。应用消息甚至允许您自定义消息的外观以及发送方式,除了默认的消息布局外。您还可以定义事件以在合适的时间触发消息发送给用户。
安装插件
在Flutter项目的pubspec.yaml
文件中添加依赖项:
dependencies:
agconnect_appmessaging:
在终端运行以下命令或在Android Studio中点击“Pub get”来添加依赖项:
flutter pub get
开发指南
以下是关于如何使用agconnect_appmessaging
插件的详细指南:
使用
参考文档:使用
参考
参考文档:参考
许可证
该插件基于以下许可证:Apache License, version 2.0。
示例代码
示例代码:example/lib/main.dart
/*
* Copyright 2021-2023. Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:flutter/material.dart';
import 'package:agconnect_appmessaging/agconnect_appmessaging.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
[@override](/user/override)
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final AGCAppMessaging _appMessaging = AGCAppMessaging.getInstance();
[@override](/user/override)
void initState() {
super.initState();
_appMessaging.onMessageDisplay.listen((AppMessage event) {
_showDialog(context, 'onMessageDisplay', event);
});
_appMessaging.onMessageDismiss.listen((AppMessage event) {
_showDialog(context, 'onMessageDismiss', event);
});
_appMessaging.onMessageClick.listen((AppMessage event) {
_showDialog(context, 'onMessageClick', event);
});
_appMessaging.onMessageError.listen((AppMessage event) {
_showDialog(context, 'onMessageError', event);
});
// Uncomment this for use custom view.
// _appMessaging.onCustomEvent.listen((AppMessage? event) async {
// _showDialog(context, 'onCustomEvent', event);
// await _appMessaging.handleCustomViewMessageEvent(
// AGCAppMessagingEventType.onMessageDismiss,
// AGCAppMessagingDismissType.CLICK,
// );
// });
}
void _showDialog(BuildContext context, String title, [dynamic content]) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: content == null
? null
: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Text('$content'),
),
);
},
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("AGC AppMessaging Demo"),
),
body: ListView(
padding: const EdgeInsets.all(16),
physics: const BouncingScrollPhysics(),
children: [
_buildGroup(
children: [
_buildButton(
text: 'isDisplayEnable',
onTap: () async => await _appMessaging.isDisplayEnable(),
),
const Divider(height: 0),
_buildButton(
text: 'Enable Display',
onTap: () async => await _appMessaging.setDisplayEnable(true),
),
_buildButton(
text: 'Disable Display',
onTap: () async => await _appMessaging.setDisplayEnable(false),
),
],
),
_buildGroup(
children: [
_buildButton(
text: 'isFetchMessageEnable',
onTap: () async => await _appMessaging.isFetchMessageEnable(),
),
const Divider(height: 0),
_buildButton(
text: 'Enable Fetch Message',
onTap: () async =>
await _appMessaging.setFetchMessageEnable(true),
),
_buildButton(
text: 'Disable Fetch Message',
onTap: () async =>
await _appMessaging.setFetchMessageEnable(false),
),
],
),
_buildGroup(
children: [
_buildButton(
text: 'setForceFetch',
onTap: () async => await _appMessaging.setForceFetch(),
),
const Divider(height: 0),
_buildButton(
text: 'setDisplayLocation\nCENTER',
onTap: () async => await _appMessaging
.setDisplayLocation(AGCAppMessagingDisplayLocation.CENTER),
),
const Divider(height: 0),
_buildButton(
text: 'removeCustomView',
onTap: () async => await _appMessaging.removeCustomView(),
),
const Divider(height: 0),
_buildButton(
text: 'trigger\n#AppOnForeground',
onTap: () async =>
await _appMessaging.trigger('#AppOnForeground'),
),
],
),
],
),
);
}
Widget _buildGroup({
required List<Widget> children,
}) {
return Container(
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.only(bottom: 16),
decoration: const BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.all(Radius.circular(16)),
),
child: Wrap(
spacing: 8,
runSpacing: 8,
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: children,
),
);
}
Widget _buildButton({
required String text,
required Future<dynamic> Function() onTap,
}) {
return ElevatedButton(
child: Text(
text,
textAlign: TextAlign.center,
),
onPressed: () async {
try {
final dynamic result = await onTap();
_showDialog(context, 'SUCCESS', result);
} catch (e) {
_showDialog(context, 'ERROR', e.toString());
}
},
);
}
}
更多关于Flutter应用消息推送插件agconnect_appmessaging的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter应用消息推送插件agconnect_appmessaging的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
agconnect_appmessaging
是华为提供的用于在 Flutter 应用中集成 App Messaging 功能的插件。App Messaging 允许开发者通过华为 AppGallery Connect 平台向用户发送应用内消息,以提升用户参与度和留存率。
以下是如何在 Flutter 应用中使用 agconnect_appmessaging
插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 agconnect_appmessaging
插件的依赖:
dependencies:
flutter:
sdk: flutter
agconnect_appmessaging: ^1.6.0+300
然后,运行 flutter pub get
以安装依赖。
2. 配置华为 AppGallery Connect
在华为 AppGallery Connect 控制台中,确保你已经创建了项目并启用了 App Messaging 功能。
3. 配置 Flutter 项目
在 android/app/build.gradle
文件中,确保你已经添加了华为的 Maven 仓库:
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
4. 初始化插件
在 main.dart
文件中初始化 agconnect_appmessaging
插件:
import 'package:flutter/material.dart';
import 'package:agconnect_appmessaging/agconnect_appmessaging.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AGCAppMessaging().init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
5. 处理消息回调
你可以通过设置回调来处理应用内消息的展示和点击事件:
AGCAppMessaging().setMessageHandler((message) {
print("App Messaging received: ${message.id}");
// 处理消息展示逻辑
});
AGCAppMessaging().setMessageClickHandler((message) {
print("App Messaging clicked: ${message.id}");
// 处理消息点击逻辑
});
6. 触发事件(可选)
你可以通过触发事件来触发特定的应用内消息。例如:
AGCAppMessaging().triggerEvent("your_event_name");