xy_umeng 1.0.2
xy_umeng: ^1.0.2 copied to clipboard
A Flutter plugin for UMeng U-App analytics and U-Link smart deeplink.
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xy_umeng/utils/xy_logger.dart';
import 'package:xy_umeng/xy_umeng.dart';
import 'package:xy_umeng/xy_umeng_platform_interface.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'XY Umeng',
home: MainWidget(),
onGenerateRoute: (RouteSettings settings) {
final uri = Uri.tryParse(settings.name ?? '');
if (uri != null && uri.path == '/invite') {
final inviteUid = uri.queryParameters['inviteUid'];
final channelCode = uri.queryParameters['channelCode'];
XYLogger.d("唤醒App,判断登录、跳转到页面:$inviteUid $channelCode");
}
return null;
},
onUnknownRoute: (RouteSettings settings) {
XYLogger.d('收到一个被忽略的未知路由: ${settings.name}');
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (Navigator.canPop(context)) {
Navigator.pop(context);
}
});
return Container();
},
);
},
);
}
}
class MainWidget extends StatefulWidget {
const MainWidget({super.key});
@override
State<MainWidget> createState() => _MainWidgetState();
}
class _MainWidgetState extends State<MainWidget> {
String _platformVersion = 'Unknown';
String _oaid = '';
String _error = '';
bool _isInit = false;
XYLinkInstallModel? _installModel;
XYLinkWakeupModel? _wakeupModel;
final _umengPlugin = XYUmeng();
@override
void initState() {
super.initState();
getPlatformVersion();
WidgetsBinding.instance.addPostFrameCallback((_) {
// 部分iOS/Android设备需要提前申请网络权限
Connectivity().checkConnectivity();
});
}
/// 初始化友盟
Future<void> initUmeng() async {
// 初始化,需要更换开发者自己的appKey
await _umengPlugin.init(androidAppKey: "6a325c46cbfa6959515f45e4", iosAppKey: "6a3260a86f259537c7bae7d6").whenComplete(() async {
_isInit = await _umengPlugin.getIsInit();
if (mounted) setState(() {});
});
// 设置
await _umengPlugin.setLogEnabled(true);
await _umengPlugin.setAutoPageEnabled(true);
await _umengPlugin.setEncryptEnabled(true);
// 监听U-Link
_umengPlugin.addLinkListener(
onInstall: (XYLinkInstallModel model) {
_installModel = model;
XYLogger.d('onInstall getData ${model.getData}');
if (mounted) setState(() {});
},
onWakeup: (XYLinkWakeupModel model) {
_wakeupModel = model;
if (mounted) setState(() {});
},
onError: (String errorMessage) {
_error = errorMessage;
if (mounted) setState(() {});
XYLogger.d('onError $errorMessage');
},
);
_umengPlugin.getOAID().then((oaid) {
XYLogger.d('OAID: $oaid');
setState(() {
_oaid = oaid;
});
});
}
/// 获取友盟U-Link安装参数
Future<void> getInstallParams() async {
Future.delayed(Duration(seconds: 2)).whenComplete(() {
_umengPlugin.getInstallParams(enablePasteboard: true);
});
}
/// 获取平台版本
Future<void> getPlatformVersion() async {
String platformVersion;
try {
platformVersion = await _umengPlugin.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('XY Umeng'),
actions: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('退出App'),
content: Text('确定要退出吗?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('取消'),
),
TextButton(
onPressed: () async {
if (Platform.isAndroid) {
await _umengPlugin.onKillProcess();
SystemNavigator.pop();
} else {
exit(0);
}
},
child: Text('确定'),
),
],
);
},
);
},
icon: Icon(Icons.exit_to_app),
),
],
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: [
Text('Running on: $_platformVersion\n'),
Text('是否初始化: $_isInit\n', style: TextStyle(color: _isInit ? Colors.green : null)),
ElevatedButton(onPressed: initUmeng, child: Text("同意隐私协议-初始化友盟")),
ElevatedButton(onPressed: getInstallParams, child: Text("延时2s获取友盟U-Link安装参数")),
Divider(),
Text.rich(
TextSpan(
text: 'U-App统计:\n',
children: [
if (Platform.isAndroid) TextSpan(text: 'OAID: \n$_oaid\n'),
TextSpan(
text: '【账号统计登录】',
style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline, decorationColor: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
_umengPlugin.signIn('UserId');
XYLogger.d('账号统计登录');
},
),
TextSpan(
text: '【账号统计登出】\n\n',
style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline, decorationColor: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
_umengPlugin.signOff();
XYLogger.d('账号统计登出');
},
),
TextSpan(
text: '【页面统计进入】',
style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline, decorationColor: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
_umengPlugin.onPageStart('Main');
XYLogger.d('页面统计进入');
},
),
TextSpan(
text: '【页面统计退出】\n\n',
style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline, decorationColor: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
_umengPlugin.onPageEnd('Main');
XYLogger.d('页面统计退出');
},
),
TextSpan(
text: '【埋点事件统计】\n',
style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline, decorationColor: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
// 统计事件 购买《Swift Fundamentals》这本书
_umengPlugin.onEvent('pay', attributes: {'type': 'book', 'name': "Swift Fundamentals"});
XYLogger.d('埋点事件统计');
},
),
],
),
),
Divider(),
Text.rich(
TextSpan(
text: 'U-Link超链数据:\n',
children: [
TextSpan(text: 'installUrl: \n${_installModel?.url}\n\n'),
TextSpan(text: 'installParams: \n${_installModel?.params}\n\n'),
TextSpan(text: 'installData: \n${_installModel?.getData}\n\n'),
TextSpan(text: 'wakeupPath: \n${_wakeupModel?.path}\n\n'),
TextSpan(text: 'wakeupParams: \n${_wakeupModel?.params}\n\n'),
TextSpan(text: 'error: \n$_error'),
],
),
),
],
),
);
}
}