← Back to posts Cover image for Unlocking Voice Commands: Integrating Google Assistant and Siri with Flutter Apps

Unlocking Voice Commands: Integrating Google Assistant and Siri with Flutter Apps

· 4 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Voice assistants like Google Assistant and Siri offer a hands-free, futuristic way for users to interact with your app. Imagine a user cooking and saying, “Hey Google, open RecipeWizard,” or driving and telling Siri to start their favorite meditation app. Integrating this functionality isn’t about writing Flutter code that listens for a wake word—it’s about teaching the operating system to associate your app with specific voice commands.

The core mechanism both platforms use is deep linking. When a user says “Open [App Name],” the OS needs to know which app to launch and what to do once it’s open. This requires configuring the native layers of your Flutter project.

Configuring Android for Google Assistant

On Android, the entry point is an Intent Filter. You define this in your AndroidManifest.xml file, located at android/app/src/main/.

You need to declare which “actions” and “categories” your app can handle. The standard action for being launched is android.intent.action.MAIN, and the category for being a launcher is android.intent.category.LAUNCHER. This is already present in your default Flutter project. To support being opened by voice, you ensure your main activity is configured to handle the VIEW action with the BROWSABLE category, which allows it to be triggered from external sources like Google Assistant.

Here’s a typical configuration for your main activity inside the <application> tag:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    <!-- Intent filter for deep linking/voice commands -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myapp" />
    </intent-filter>
</activity>

The key addition is the second <intent-filter>. It declares that your app can handle VIEW actions for URLs starting with myapp://. This is your custom deep link scheme. With this, the command “Hey Google, open MyApp” should work, as Google Assistant will attempt to launch an app that can handle a myapp:// URL.

Configuring iOS for Siri

iOS uses a similar concept called URL Schemes. You define these in your Info.plist file, located at ios/Runner/.

You need to add a CFBundleURLTypes entry. This can be done directly in Xcode by opening the ios/Runner.xcworkspace or by editing the Info.plist file as raw XML.

Here’s how the relevant section looks in the Info.plist file:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string>
        </array>
    </dict>
</array>

This tells iOS that your app registers for the myapp:// URL scheme. Now, when Siri processes the command “Hey Siri, open MyApp,” it will know to launch your app.

Once the native layer launches your app via a deep link (like myapp://), you need to catch that link within your Flutter code and route the user appropriately. The uni_links package is excellent for this.

First, add the dependency to your pubspec.yaml:

dependencies:
  uni_links: ^0.5.1

Then, in your app’s main entry point, you can set up a listener. A robust approach is to use it in your root widget’s initState.

import 'package:uni_links/uni_links.dart';
import 'package:flutter/services.dart' show PlatformException;

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Uri? _initialUri;
  Uri? _latestUri;

  @override
  void initState() {
    super.initState();
    _initUniLinks();
  }

  Future<void> _initUniLinks() async {
    // Handle initial link (app was cold-started by the link)
    try {
      final initialUri = await getInitialUri();
      if (initialUri != null) {
        setState(() => _initialUri = initialUri);
        _navigateBasedOnLink(initialUri);
      }
    } on PlatformException catch (e) {
      // Handle exception
    }

    // Listen for links while the app is running
    uriLinkStream.listen((Uri? uri) {
      if (uri != null) {
        setState(() => _latestUri = uri);
        _navigateBasedOnLink(uri);
      }
    });
  }

  void _navigateBasedOnLink(Uri uri) {
    // Example: myapp://home or myapp://profile/123
    if (uri.path.startsWith('/profile')) {
      final id = uri.pathSegments.length > 1 ? uri.pathSegments[1] : null;
      // Use your router (e.g., GoRouter, Navigator) to go to profile page
      // Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage(id: id)));
    }
    // Add more route handling logic
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Voice Command Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Initial Deep Link: ${_initialUri?.toString() ?? "None"}'),
            Text('Latest Deep Link: ${_latestUri?.toString() ?? "None"}'),
          ],
        ),
      ),
    );
  }
}

Common Pitfalls and Testing

  1. Caching: After making native configuration changes, do a full reinstall (flutter run or a fresh install from the store). The OS caches app capabilities.
  2. Scheme Uniqueness: Use a unique URL scheme (like myappname123) to avoid conflicts with other apps.
  3. Testing: On Android, you can test with adb:
    adb shell am start -a android.intent.action.VIEW -d "myapp://home"
    On iOS, you can test by typing your custom URL (myapp://home) directly into Safari’s address bar, which should prompt to open your app.

By configuring these native hooks and using uni_links to handle the incoming data, you enable users to launch your Flutter app with a simple voice command.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter is gaining traction as a robust framework for Linux desktop development, offering a smoother experience compared to traditional toolkits like GTK and Qt. This post will explore why Flutter excels for Linux apps, highlight its advantages, and provide practical insights for developers looking to leverage Flutter for their desktop projects.

Cover image for Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Deploying Flutter desktop apps on Windows can be challenging, especially regarding code signing and preventing Windows from blocking installations. This post will guide developers through the process of signing their Flutter desktop applications, exploring alternatives to the Microsoft Store like creating executable installers, and covering the necessary steps to ensure a smooth user experience.

Cover image for Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Developers frequently voice frustrations about Flutter SDK version management and installation processes, particularly on Windows. This post will explore best practices for managing multiple Flutter versions using tools like FVM, discuss integrating Flutter with package managers like Winget for a smoother Windows setup, and offer strategies to ensure a consistent development environment across projects and teams.