Note
You can configure the Airbridge Flutter SDK to handle Airbridge-related tasks occurring within the in-app website of a hybrid app without modifying the website's code.
The Flutter SDK can handle commands related to event transmission, device settings, and user settings that occur within the Web SDK. Before displaying the website in the web view, configure the Flutter SDK using the Airbridge.createWebInterfaceScript
and Airbridge.handleWebInterfaceCommand
functions.
webToken
: This is the Web SDK token. Your Web SDK token can be found on the [Settings]>[Tokens] page in the Airbridge dashboard.
postMessageScript
: This JavaScript code transmits the payload variable, which stores the command passed from the Web SDK to the Flutter SDK, to the Flutter area.
command
: This command is passed from the Web SDK to the Flutter SDK.
import 'dart:collection';
import 'package:airbridge_flutter_sdk/airbridge_flutter_sdk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class TestPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return TestState();
}
}
class TestState extends State<TestPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getJavascriptInterface(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return InAppWebView(
initialUrlRequest: URLRequest(url: WebUri('YOUR_WEB_URL')),
onWebViewCreated: (controller) async {
controller.addJavaScriptHandler(handlerName: 'postMessage', callback: (args) {
Airbridge.handleWebInterfaceCommand(args[0]);
return args;
});
},
initialUserScripts: UnmodifiableListView<UserScript>([
UserScript(
source: snapshot.requireData ?? "",
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START)
]),
)
});
}
Future<String?> getJavascriptInterface() async {
return await Airbridge.createWebInterfaceScript(
webToken: 'YOUR_WEB_SDK_TOKEN',
postMessageScript: 'window.flutter_inappwebview.callHandler("postMessage", payload);',
);
}
}
启用 Hybrid App 设置后,应仅通过 Web SDK 收集在应用内 WebView 中发生的事件。如果同时使用 App SDK 收集事件,可能会导致重复统计。
如果应用内 WebView 使用的是实际的移动版网站,请仅通过 Web SDK 收集事件。
Was this page helpful?