Flutter功能扩展插件flutter_upshot_plugin的使用
Flutter功能扩展插件flutter_upshot_plugin的使用
Upshot.ai 是一个分析和客户互动平台。此框架帮助您捕获分析数据、跟踪事件、发送智能通知和应用内消息给用户。
开始使用
本项目是一个用于 Flutter 的插件包起点,它是一种专门的包,包含适用于 Android 和/或 iOS 的平台特定实现代码。
对于如何开始使用 Flutter,请参阅我们的 在线文档,其中提供了教程、示例、移动开发指南以及完整的 API 参考。
示例代码
以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 flutter_upshot_plugin
插件。
import 'dart:async';
import 'dart:collection';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_upshot_plugin/flutter_upshot_plugin.dart';
import 'package:flutter_upshot_plugin/upshot_constants.dart';
void main() {
runApp(const DemoApp());
}
class DemoApp extends StatelessWidget {
const DemoApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
[@override](/user/override)
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
initializeBrandKinesisWithOptions();
}
Future<void> initPlatformState() async {
try {
// 获取 SDK 版本
// String? platformVersion = await FlutterUpShotPlugin.getSDKVersion ?? 'Unknown platform version';
// if (!mounted) return;
// setState(() {
// _platformVersion = platformVersion!;
// });
} catch (e) {
log('Error: $e');
}
}
Future<void> initializeBrandKinesisWithOptions() async {
Map<String, dynamic> optionsMap = {
UpshotInitOptions.appId: "e748a45e-fbef-4a7e-a2c7-ef0b88812399",
UpshotInitOptions.ownerId: "f3bf1d6f-5771-41f7-a6ff-640d3af4805e",
UpshotInitOptions.enableDebuglogs: false,
UpshotInitOptions.enableLocation: false,
UpshotInitOptions.enableCrashlogs: true,
UpshotInitOptions.enableExternalStorage: false
};
// 初始化 Upshot
FlutterUpShotPlugin.initialiseUpshotUsingOptions(optionsMap);
if (kDebugMode) {
print("******* registerForPushNotifications ***********");
}
// 注册推送通知
FlutterUpShotPlugin.registerForPushNotifications();
}
Future<void> createEvent(String eventName, HashMap<String, Object> data) async {
try {
// 创建自定义事件
// String? eventID = await FlutterUpShotPlugin.createCustomEvent(eventName, data, false);
// log('$eventID');
} catch (e) {
log('Error : $e');
}
}
Future<void> createLocationEvent(double lat, double long) async {
try {
// 创建位置事件
// await FlutterUpShotPlugin.createLocationEvent(lat, long);
} catch (e) {
log('$e');
}
}
Future<void> createAttributionEvent(String attributionSource, String utmSource, String utmMedium, String utmCampaign) async {
try {
Map<String, dynamic> optionsMap = {
UpshotAttribution.attributionSource.toString(): attributionSource,
UpshotAttribution.utmSource.toString(): utmSource,
UpshotAttribution.utmMedium.toString(): utmMedium,
UpshotAttribution.utmCampaign.toString(): utmCampaign
};
await FlutterUpShotPlugin.createAttributionEvent(optionsMap);
} catch (e) {
log('$e');
}
}
static void sendUserDetails(HashMap<String, Object> data) {
Map<String, dynamic> profileData = {
UpshotProfileAttributes.email: "upshot@gmail.com",
UpshotProfileAttributes.userName: "Upshot",
UpshotProfileAttributes.appuID: "Vinod"
};
// 发送用户详细信息
FlutterUpShotPlugin.sendUserDetails(profileData);
}
static Future<void> setValueAndClose(String eventName, Map<String, dynamic> data) async {
// 设置值并关闭事件
// await FlutterUpShotPlugin.setValueAndClose(eventName, data);
}
static Future<void> closeEventForId(String eventId) async {
// 关闭指定事件ID的事件
// await FlutterUpShotPlugin.closeEventForId(eventId);
}
static Future<void> dispatchEventWithTime(bool time) async {
// 分发事件
// await FlutterUpShotPlugin.dispatchEvents(time);
}
static void showInbox() {
// 显示收件箱
FlutterUpShotPlugin.getUnreadNotificationsCount(3);
Map<String, dynamic> options = {
UpshotInboxScreenConfig.inboxType: UpshotInboxType.both,
UpshotInboxScreenConfig.displayMessageCount: true,
UpshotInboxScreenConfig.displayTime: true,
UpshotInboxScreenConfig.enableLoadMore: true,
UpshotInboxScreenConfig.pushFetchLimit: 50,
UpshotInboxScreenConfig.showReadNotifications: true
};
FlutterUpShotPlugin.showInboxScreen(options);
}
static Future<void> createPageViewEvent(String pageName) async {
try {
String? eventID = await FlutterUpShotPlugin.createPageViewEvent(pageName);
log(eventID.toString());
} catch (e) {
log('Error : $e');
}
}
Future<void> terminateUpshot() async {
// 终止 Upshot
// await FlutterUpShotPlugin.terminateUpshot();
}
Future<void> showActivity(String tag) async {
FlutterUpShotPlugin.showActivity(-1, "");
}
static Future<void> getBadges() async {
// 获取用户徽章
// await FlutterUpShotPlugin.fetchUserBadges();
}
static Future<void> getCampaignDetails() async {
// 获取活动详情
// await FlutterUpShotPlugin.fetchInboxDetails();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Upshot Plugin Example'),
),
body: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(
shrinkWrap: true,
children: [
const SizedBox(height: 20),
Center(
child: Text('Running on: $_platformVersion\n'),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
initializeBrandKinesisWithOptions();
},
child: const Text("Initialize With Options"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () async {
HashMap<String, Object>? data = HashMap();
data['city'] = 'Bengaluru';
data['timesVisited'] = 20;
createEvent("test", data);
},
child: const Text("Create Event"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () async {
createLocationEvent(17.2365, 25.3269);
},
child: const Text("Create Location Event"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
terminateUpshot();
},
child: const Text("Terminate Upshot"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
HashMap<String, Object> data = HashMap();
data.putIfAbsent("first_name", () => "G S Prakash");
data.putIfAbsent("age", () => 23);
data.putIfAbsent("gender", () => 1);
data.putIfAbsent("mail", () => "gsp8672@gmail.com");
data.putIfAbsent("day", () => 23);
data.putIfAbsent("month", () => 3);
data.putIfAbsent("year", () => 1996);
data.putIfAbsent("appUID", () => "GFKB6598BV");
data.putIfAbsent("facebookId", () => "some URL");
data.putIfAbsent("twitterId", () => "some URL");
data.putIfAbsent("city", () => "Bangalore");
data.putIfAbsent("state", () => "Karnataka");
sendUserDetails(data);
},
child: const Text("Send User Details"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
getBadges();
},
child: const Text("Get Badges"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
closeEventForId('ffa1d44d-b0d6-48e3-a9f6-ae2481d90996\$c');
},
child: const Text("Close Event for ID"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
HashMap<String, Object>? data = HashMap();
data['city'] = 'Bengaluru';
data['timesVisited'] = 20;
setValueAndClose("test", data);
},
child: const Text("SetValue And Close"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
createPageViewEvent("Login");
},
child: const Text("Create page view event"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
getCampaignDetails();
},
child: const Text("Get VisualInbox"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
dispatchEventWithTime(true);
},
child: const Text("Dispatch event with time"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
showInbox();
},
child: const Text("Show Inbox"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
showActivity("main");
},
child: const Text("Show Activity"),
),
const SizedBox(height: 10),
TextButton(
style: TextButton.styleFrom(
backgroundColor: Colors.black12,
textStyle: const TextStyle(color: Colors.white)),
onPressed: () {
createAttributionEvent(
"attribution", "utmSource", "utmMedium", "utmCampaign");
},
child: const Text("Create Attribution Event"),
),
const SizedBox(height: 40),
],
),
),
);
}
}
更多关于Flutter功能扩展插件flutter_upshot_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter功能扩展插件flutter_upshot_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_upshot_plugin
是一个用于在 Flutter 应用中集成 Upshot 功能的插件。Upshot 是一个用户分析和营销自动化平台,可以帮助开发者更好地了解用户行为、推送个性化通知、进行 A/B 测试等。
1. 安装插件
首先,你需要在 pubspec.yaml
文件中添加 flutter_upshot_plugin
依赖:
dependencies:
flutter:
sdk: flutter
flutter_upshot_plugin: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
2. 配置 Upshot
在 Android 和 iOS 平台上,你需要进行一些配置。
Android 配置
在 android/app/src/main/AndroidManifest.xml
文件中添加以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在 android/app/build.gradle
文件中添加 Upshot 的依赖:
dependencies {
implementation 'com.upshot.android:analytics:1.0.0' // 请使用最新版本
}
iOS 配置
在 ios/Podfile
中添加 Upshot 的依赖:
pod 'Upshot', '~> 1.0.0' # 请使用最新版本
然后运行 pod install
来安装依赖。
3. 初始化 Upshot
在你的 Flutter 应用中,你需要在应用启动时初始化 Upshot。通常可以在 main.dart
文件中进行初始化:
import 'package:flutter/material.dart';
import 'package:flutter_upshot_plugin/flutter_upshot_plugin.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Upshot
FlutterUpshotPlugin.initialize(
appId: 'YOUR_APP_ID',
ownerId: 'YOUR_OWNER_ID',
apiKey: 'YOUR_API_KEY',
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Upshot Demo',
home: HomeScreen(),
);
}
}
4. 使用 Upshot 功能
记录用户事件
你可以使用 FlutterUpshotPlugin
来记录用户事件:
FlutterUpshotPlugin.logEvent(eventName: 'button_clicked', properties: {'button_id': 'login_button'});
设置用户属性
你可以设置用户的属性,例如用户 ID、姓名、电子邮件等:
FlutterUpshotPlugin.setUserAttributes(attributes: {
'user_id': '12345',
'name': 'John Doe',
'email': 'john.doe@example.com',
});
推送通知
Upshot 还支持推送通知功能。你可以在 Upshot 控制台配置通知,并在应用中接收和处理通知。
5. 处理通知
你可以在 main.dart
中设置通知处理逻辑:
FlutterUpshotPlugin.setNotificationHandler((Map<String, dynamic> notification) {
// 处理通知
print('Received notification: $notification');
});
6. 调试和测试
在开发过程中,你可以启用 Upshot 的调试模式来查看日志:
FlutterUpshotPlugin.setDebugMode(enable: true);