Unlocking Voice Commands: Integrating Google Assistant and Siri with Flutter Apps
The Flutter news you actually need
No spam, ever. Unsubscribe in one click.
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.
Handling the Deep Link in Flutter
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
- Caching: After making native configuration changes, do a full reinstall (
flutter runor a fresh install from the store). The OS caches app capabilities. - Scheme Uniqueness: Use a unique URL scheme (like
myappname123) to avoid conflicts with other apps. - Testing: On Android, you can test with
adb:
On iOS, you can test by typing your custom URL (adb shell am start -a android.intent.action.VIEW -d "myapp://home"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
Flutter App Size Optimization: Strategies to Shrink Your APK/IPA Below 10MB
Flutter apps are often criticized for their larger-than-native binary sizes. This post will dive into practical, actionable strategies to significantly reduce your Flutter app's footprint, including build configurations, asset optimization, and dependency management, demonstrating how to achieve app sizes under 10MB.
Mastering In-App Subscriptions in Flutter: A Guide to Avoiding Common Pitfalls
Implementing in-app subscriptions in Flutter can be surprisingly complex, especially when dealing with cross-platform nuances, receipt validation, and managing different store configurations. This post will delve into the common challenges developers face, compare solutions like RevenueCat with custom implementations, and provide best practices for setting up and maintaining a robust subscription system.
Taming Android Emulator RAM: A Developer's Guide to Reducing Memory Usage for Flutter
Android emulators are notorious for hogging RAM, often pushing developer machines into swap and slowing down Flutter development. This post will explore the reasons behind high emulator memory consumption and provide practical, actionable strategies and tools (including a custom-built solution) to significantly reduce RAM usage, ensuring a smoother development experience without sacrificing functionality.