Flutter即时通讯插件intercom_android的使用
Flutter即时通讯插件intercom_android的使用
该Android实现是intercom_flutter_plugin
的一部分。
使用
这个包是被支持的,这意味着你可以像平常一样使用intercom_flutter_plugin
。当你这样做时,这个包将自动包含在你的应用中,因此你不需要在pubspec.yaml
文件中添加它。
然而,如果你导入此包以直接使用其任何API,则应像往常一样将其添加到你的pubspec.yaml
文件中。
示例代码
以下是使用intercom_flutter_plugin
的完整示例代码:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:intercom_platform_interface/intercom_platform_interface.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// TODO: 确保添加来自您的Intercom工作区的密钥。
await IntercomPlatform.instance.initialize(
appId: 'your_app_id',
androidApiKey: 'your_android_api_key',
);
runApp(const MaterialApp(home: HomePage()));
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
[@override](/user/override)
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
StreamSubscription<dynamic>? _streamSubscription;
[@override](/user/override)
void dispose() {
_streamSubscription?.cancel();
super.dispose();
}
[@override](/user/override)
void initState() {
super.initState();
_streamSubscription = IntercomPlatform.instance.getUnreadStream().listen(
(event) => debugPrint('UnreadStream EVENT: $event'),
onError: (e, stack) => debugPrint('UnreadStream ERROR: $e; stack: $stack'),
onDone: () => debugPrint('UnreadStream DONE'),
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Intercom示例应用'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () {
// 登出
IntercomPlatform.instance.logout();
// 登录已识别用户
IntercomPlatform.instance.loginIdentifiedUser(
userId: '123456789',
email: 'example@gmail.com',
statusCallback: IntercomStatusCallback(
onSuccess: () async {
// 显示消息中心
await IntercomPlatform.instance.displayMessenger();
// 5秒后隐藏消息中心
await Future.delayed(const Duration(seconds: 5), () {
IntercomPlatform.instance.hideMessenger();
});
},
onFailure: (IntercomError error) {
// 失败回调
debugPrint(error.toString());
},
),
);
},
child: const Text('显示消息中心'),
),
TextButton(
onPressed: () {
// 设置启动器可见
IntercomPlatform.instance.setLauncherVisibility(IntercomVisibility.visible);
},
child: const Text('显示启动器'),
),
TextButton(
onPressed: () {
// 设置启动器不可见
IntercomPlatform.instance.setLauncherVisibility(IntercomVisibility.gone);
},
child: const Text('隐藏启动器'),
),
TextButton(
onPressed: () {
// 显示帮助中心
IntercomPlatform.instance.displayHelpCenter();
},
child: const Text('显示帮助中心'),
),
],
),
),
);
}
}
更多关于Flutter即时通讯插件intercom_android的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter即时通讯插件intercom_android的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
intercom_android
是一个用于在 Flutter 应用中集成 Intercom 即时通讯功能的插件。Intercom 是一个流行的客户沟通平台,允许开发者在应用中嵌入实时聊天、帮助文档、用户调查等功能。
以下是如何在 Flutter 项目中使用 intercom_android
插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 intercom_android
插件的依赖:
dependencies:
flutter:
sdk: flutter
intercom_flutter: ^4.0.0
2. 配置 Android 项目
在 android/app/build.gradle
文件中,确保 minSdkVersion
至少为 21
:
android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
// 其他配置
}
}
3. 初始化 Intercom
在 Flutter 应用启动时初始化 Intercom。通常可以在 main.dart
文件中进行初始化:
import 'package:flutter/material.dart';
import 'package:intercom_flutter/intercom_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Intercom
await Intercom.initialize(
'YOUR_APP_ID', // 你的 Intercom App ID
androidApiKey: 'YOUR_ANDROID_API_KEY', // 你的 Android API Key
iosApiKey: 'YOUR_IOS_API_KEY', // 你的 iOS API Key
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Intercom Example',
home: HomeScreen(),
);
}
}
4. 注册用户
在用户登录后,可以使用以下代码将用户注册到 Intercom:
await Intercom.registerIdentifiedUser(userId: 'USER_ID');
你还可以添加用户的其他信息,如电子邮件、姓名等:
await Intercom.updateUser(
email: 'user@example.com',
name: 'User Name',
phone: '+1234567890',
);
5. 显示聊天窗口
你可以在应用中的任何地方显示 Intercom 的聊天窗口:
await Intercom.displayMessenger();
6. 处理推送通知
为了在 Android 设备上接收 Intercom 的推送通知,你需要在 AndroidManifest.xml
文件中添加以下配置:
<application>
<service
android:name="io.intercom.android.sdk.push.IntercomPushService"
android:exported="true" />
<receiver
android:name="io.intercom.android.sdk.push.IntercomPushBroadcastReceiver"
android:exported="true" />
</application>
7. 注销用户
当用户注销时,记得注销 Intercom 用户:
await Intercom.logout();