Flutter客服集成插件help_scout_beacon的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter客服集成插件help_scout_beacon的使用

Help Scout Beacon SDK for Flutter

Streamline customer communications in your app with the Help Scout Beacon SDK for Flutter.

Usage

Quick start

Configure the beacon with your Beacon ID and launch the beacon UI by calling open.

final HelpScoutBeacon beacon = HelpScoutBeacon(HSBeaconSettings(beaconId: 'YOUR_BEACON_ID'));
beacon.open();

Identify User

Optionally, you can prefill the beacon with user metadata:

final settings = HSBeaconSettings(beaconId: 'YOUR_BEACON_ID', debugLogging: true);
final HelpScoutBeacon beacon = HelpScoutBeacon(settings);

final user = HSBeaconUser(email: "john.doe@example.com", name: "John Doe");
beacon.identify(beaconUser: user);
beacon.open();

Navigate

Open a desired page in the Help Scout beacon UI:

beacon.open(route: HSBeaconRoute.ask);
beacon.open(route: HSBeaconRoute.chat);
beacon.open(route: HSBeaconRoute.contactForm);
beacon.open(route: HSBeaconRoute.previousMessages);
beacon.open(route: HSBeaconRoute.docs);
beacon.open(route: HSBeaconRoute.docs, parameter: 'search term');
beacon.open(route: HSBeaconRoute.article, parameter: 'article id');

Cleanup / Logout

Once done, you can remove all data:

beacon.clear();

Release mode

The newest version on Android requires setting up pro-guard rules. See example project proguard-rules.pro.

示例代码

以下是一个完整的示例代码,展示了如何在Flutter应用中集成和使用Help Scout Beacon SDK。

import 'package:flutter/material.dart';
import 'package:help_scout_beacon/help_scout_beacon.dart';
import 'package:help_scout_beacon/help_scout_beacon_api.g.dart';

/// YOUR HELPSCOUT BEACON ID
const String yourBeaconId = "YOUR_BEACON_ID";

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _formKey = GlobalKey<FormState>();
  final HelpScoutBeacon beacon = HelpScoutBeacon(
      HSBeaconSettings(beaconId: yourBeaconId, debugLogging: true));

  @override
  Widget build(BuildContext context) {
    const spacer = SizedBox(height: 16, width: 16);

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Help Scout Plugin'),
        ),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Enter your beacon id
              const Text("BeaconId: \n$yourBeaconId"),

              spacer,

              // Start a beacon ui
              ElevatedButton(
                onPressed: () => beacon.open(),
                child: const Text('Open (Default)'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () => beacon.open(route: HSBeaconRoute.ask),
                child: const Text('Open Ask'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () => beacon.open(route: HSBeaconRoute.chat),
                child: const Text('Open Chat'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () => beacon.open(route: HSBeaconRoute.docs),
                child: const Text('Open Docs'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () =>
                    beacon.open(route: HSBeaconRoute.docs, parameter: 'Help'),
                child: const Text('Open Docs (+search term)'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () => beacon.open(route: HSBeaconRoute.contactForm),
                child: const Text('Open Contact Form'),
              ),
              spacer,

              ElevatedButton(
                onPressed: () => beacon.open(route: HSBeaconRoute.previousMessages),
                child: const Text('Open Previous Messages'),
              ),

              const SizedBox(height: 32),

              // Clear everything
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FilledButton(
                    onPressed: () => beacon.identify(
                        beaconUser: HSBeaconUser(
                            email: "john.doe@example.com", name: "John Doe")),
                    child: const Text('Set User'),
                  ),
                  const SizedBox(width: 16),
                  FilledButton(
                    onPressed: clearBeacon,
                    child: const Text('Clear'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void>? clearBeacon() async {
    await beacon.clear();
  }

  bool isFormValid() {
    return _formKey.currentState?.validate() ?? false;
  }
}

说明

  1. 配置Beacon ID:在yourBeaconId常量中填入你的Help Scout Beacon ID。
  2. 初始化Beacon:在_MyAppState类中初始化HelpScoutBeacon对象,并传入配置。
  3. 按钮操作:通过不同的按钮调用beacon.open方法,打开不同的页面或功能。
  4. 清理数据:通过beacon.clear方法清除所有数据。

希望这个示例能帮助你更好地理解和使用Help Scout Beacon SDK for Flutter。如果有任何问题,欢迎随时提问!


更多关于Flutter客服集成插件help_scout_beacon的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter客服集成插件help_scout_beacon的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中集成和使用help_scout_beacon插件的示例代码案例。这个插件允许你将Help Scout的Beacon客服功能集成到你的Flutter应用中。

1. 添加依赖

首先,你需要在你的pubspec.yaml文件中添加help_scout_beacon依赖:

dependencies:
  flutter:
    sdk: flutter
  help_scout_beacon: ^最新版本号 # 请替换为实际最新版本号

然后运行flutter pub get来安装依赖。

2. 配置插件

在你的Flutter应用的main.dart文件中,你需要配置并初始化help_scout_beacon插件。通常,你会在应用的MaterialAppCupertinoApp的顶层进行初始化。

import 'package:flutter/material.dart';
import 'package:help_scout_beacon/help_scout_beacon.dart';

void main() {
  // 初始化Help Scout Beacon
  HelpScoutBeacon.initialize(
    beaconId: '你的Beacon ID', // 替换为你的Beacon ID
    domain: '你的Help Scout域名', // 替换为你的Help Scout域名,例如 'yourcompany.helpscout.net'
  );

  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 Demo Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '打开客服聊天',
            ),
            ElevatedButton(
              onPressed: () {
                // 打开Help Scout Beacon聊天窗口
                HelpScoutBeacon.openChat();
              },
              child: Text('开始聊天'),
            ),
          ],
        ),
      ),
    );
  }
}

3. 处理事件(可选)

help_scout_beacon插件可能还提供了处理聊天事件的方法,比如监听聊天窗口的打开或关闭事件。这些功能可以根据插件的具体文档进行实现。以下是一个假设的示例代码,用于监听聊天窗口的打开事件(具体方法名和参数请参考插件的官方文档):

// 假设存在一个监听聊天窗口打开的方法
HelpScoutBeacon.addListener((event) {
  if (event == ChatWindowEvent.opened) {
    print('聊天窗口已打开');
  }
});

请注意,上面的addListener方法和ChatWindowEvent是假设的,你需要根据help_scout_beacon插件的实际API文档进行调整。

4. 运行应用

现在,你可以运行你的Flutter应用,点击“开始聊天”按钮,应该会打开Help Scout的Beacon聊天窗口。

注意事项

  • 确保你已经正确配置了Help Scout的Beacon,并获得了必要的beaconIddomain
  • 仔细阅读help_scout_beacon插件的官方文档,以确保你使用了正确的方法和属性。
  • 根据需要处理用户隐私和数据保护的相关法规。

这个示例提供了一个基本的集成框架,你可以根据具体需求进一步定制和扩展功能。

回到顶部