Flutter集成SDK插件singular_flutter_sdk的使用
Flutter集成SDK插件singular_flutter_sdk的使用
目录
支持的平台
- Android
- iOS
插件版本
- iOS SingularSDK v12.6.0
- Android SingularSDK v12.6.0
基本集成
在Flutter项目中添加Singular SDK插件,首先需要在pubspec.yaml
文件中添加以下依赖:
dependencies:
singular_flutter_sdk: ^1.5.1
然后在项目的终端运行以下命令来获取包:
flutter packages get
在初始化Singular SDK之前,您需要创建一个包含您的API密钥和API密钥的秘密的SingularConfig
对象。您可以选择启用各种SDK功能。
示例代码:
import 'package:singular_flutter_sdk/singular.dart';
import 'package:singular_flutter_sdk/singular_config.dart';
void main() {
SingularConfig config = new SingularConfig('API_KEY', 'API_SECRET');
config.customUserId = "test@test.com";
Singular.start(config);
}
跟踪事件
您可以使用event
和eventWithArgs
方法向Singular发送事件。
示例代码:
Singular.event("eventName");
Singular.eventWithArgs("eventName", {"level-up": "5"});
跟踪收入
报告自定义事件到Singular或报告应用内购买事件。
示例代码:
报告自定义收入事件
Singular.customRevenue("MyCustomRevenue", "USD", 5.50);
报告应用内购买事件
import 'package:singular_flutter_sdk/singular_iap.dart';
// For iOS
final singularPurchaseIOS = SingularIOSIAP(
product.price,
product.currencyCode,
purchase.productId,
purchase.purchaseId,
purchase.verificationData.serverVerificationData
);
// For Android
final singularPurchaseAndroid = SingularAndroidIAP(
product.price,
product.currencyCode,
purchase.signature,
purchase.verificationData.serverVerificationData
);
Singular.inAppPurchase("purchaseEventName", singularPurchaseIOS); // or singularPurchaseAndroid for Android
实现深度链接
为了启用Singular Links,在iOS和Android上查看Singular Links Prerequisites。
处理Singular链接的示例代码:
SingularConfig config = new SingularConfig('API_KEY', 'API_SECRET');
config.singularLinksHandler = (SingularLinkParams params) {
String deeplink = params.deeplink;
String passthrough = params.passthrough;
bool isDeferred = params.isDeferred;
Map urlParameters = params.urlParameters;
// 在这里添加处理深度链接的代码
};
Singular.init(config);
iOS先决条件
Objective-C
在AppDelegate.m中添加以下内容:
#import "SingularAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
[SingularAppDelegate shared].launchOptions = launchOptions;
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *restorableObjects))restorationHandler {
[[SingularAppDelegate shared] continueUserActivity:userActivity restorationHandler:restorationHandler];
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
[[SingularAppDelegate shared] handleOpenUrl:url options:options];
return YES;
}
Swift
import singular_flutter_sdk
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if let singularAppDelegate = SingularAppDelegate.shared() {
singularAppDelegate.launchOptions = launchOptions
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
if let singularAppDelegate = SingularAppDelegate.shared() {
singularAppDelegate.continueUserActivity(userActivity, restorationHandler:nil)
}
return super.application(application, continue:userActivity, restorationHandler: restorationHandler)
}
override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let singularAppDelegate = SingularAppDelegate.shared() {
singularAppDelegate.handleOpen(url, options: options)
}
return true
}
Android先决条件
Java
在MainActivity.java中添加以下内容:
import com.singular.flutter_sdk.SingularBridge;
@Override
protected void onNewIntent(@NonNull Intent intent) {
super.onNewIntent(intent);
SingularBridge.onNewIntent(intent);
}
Kotlin
import com.singular.flutter_sdk.SingularBridge
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
SingularBridge.onNewIntent(intent);
}
添加SKAdNetwork支持
从Singular Flutter SDK版本1.0.15开始,默认情况下启用了skAdNetworkEnabled
。要手动启用SKAdNetwork跟踪,请在初始化Singular之前打开配置选项。
示例代码:
SingularConfig config = new SingularConfig('API_KEY', 'API_SECRET');
config.skAdNetworkEnabled = true;
config.manualSkanConversionManagement = true; // 启用手动转换值更新
config.conversionValueUpdatedCallback = (int conversionValue) {
print('Received conversionValueUpdatedCallback: ' + conversionValue.toString());
};
Singular.init(config);
检索转换值:
Singular.skanGetConversionValue().then((conversionValue) {
print('conversion value: ' + conversionValue.toString());
});
跟踪卸载
发送APNS/FCM令牌给Singular以跟踪应用程序卸载情况。
示例代码:
// iOS
Singular.registerDeviceTokenForUninstall(apnsToken);
// Android
Singular.registerDeviceTokenForUninstall(fcmToken);
更多关于Flutter集成SDK插件singular_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter集成SDK插件singular_flutter_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter项目中集成和使用singular_flutter_sdk
插件,可以按照以下步骤进行。这个插件通常用于跟踪和分析应用内的用户活动。以下是一个基本的集成和使用的代码示例。
1. 添加依赖
首先,在pubspec.yaml
文件中添加singular_flutter_sdk
依赖:
dependencies:
flutter:
sdk: flutter
singular_flutter_sdk: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
2. 初始化SDK
在你的应用的主入口文件(通常是main.dart
)中,初始化Singular SDK。
import 'package:flutter/material.dart';
import 'package:singular_flutter_sdk/singular_flutter_sdk.dart';
void main() {
// 初始化Singular SDK
SingularFlutterSdk.init(
apiKey: "你的API_KEY", // 替换为你的Singular API Key
userId: "用户ID", // 如果有的话,可以设置一个用户ID
debugMode: true // 在开发阶段可以设置为true,生产环境应设置为false
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Singular SDK Demo'),
),
body: Center(
child: Text('Hello, Singular SDK!'),
),
);
}
}
3. 使用SDK进行事件跟踪
你可以在应用内的不同位置使用Singular SDK来跟踪用户事件。例如,在按钮点击事件中:
import 'package:flutter/material.dart';
import 'package:singular_flutter_sdk/singular_flutter_sdk.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Singular SDK Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hello, Singular SDK!'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// 跟踪一个自定义事件
SingularFlutterSdk.trackEvent(
eventName: "button_click",
eventProperties: {
"button_name": "my_button",
"timestamp": DateTime.now().toIso8601String()
}
);
},
child: Text('Track Event'),
),
],
),
),
);
}
}
4. 清理和释放资源(可选)
在应用的适当位置(例如AppLifecycleState.detached
时),你可以调用SDK的清理方法(如果有提供的话)。不过,这通常不是必需的,因为Flutter框架会在应用关闭时自动清理资源。
注意事项
- 确保你已经在Singular平台上注册了你的应用,并获取了正确的API Key。
- 在生产环境中,记得将
debugMode
设置为false
。 - 查阅Singular的官方文档,以获取更多关于事件跟踪和配置的信息。
这个示例展示了如何在Flutter项目中集成和使用singular_flutter_sdk
插件进行基本的事件跟踪。根据你的具体需求,你可能需要调整初始化和事件跟踪的代码。