“Ok Google, use my Flutter app!”
In this post I would like to show you how I integrated Google Assistant/Google Now with my WeightTracker app written in Flutter (works only on Android).
First, let’s see what we are trying to accomplish:
(I had to type in the message because the recording tool was capturing mic input, so Google Assistant couldn’t hear what I was saying)
How to achieve this? Let’s walk through it!
Let your app catch the intent.
Google Assistant actions are based on Voice Actions. If we want to integrate your app with it we need to pick one from the list (you can find list of Voice Actions here). After we choose an action (for me it was Take a note) we can add a proper intent-filter to our AndroidManifest.xml:
. . .
Handle the intent
When our app is able to catch the intent, we need to handle it in MainActivity. For my Note intent it looks like this:
What happens here is if we get an ACTION_CREATE_NOTE intent and it is a text, we simply extract the value and store it in the private field.
. . .
Expose note to Dart
Since we cannot explicitly tell our Flutter app that we have a note, we can only return it when we are asked for it. To do that all we have to do is add new MethodChannel in MainActivity:
When MethodChannel “getSavedNote” is being invoked, we return savedNote and then set it to null.
. . .
Ask for data
You can do it in many ways, I stayed with the approach proposed on Flutter page.
Inside onInit function, I simply invoke MethodChannel method for saved note:
If there is any note saved I try to find a number there (I know it’s not perfect) and then return it. According to our app architecture, we can store that value in the database, use it in setState or do whatever we want.
When it comes to coding we’re all done. However saying “Ok Google, save a note my weight is 80” or just“Ok Google, note to self 80” may not work. We have to choose our app as a default one to use it. To do that with Google Assistant you need to say “Save a note” and then press default app’s icon and choose WeightTracker (present on the right).
. . .
And that’s it!
- You can say “save a note blabla in Evernote” and it will pick Evernote. However, if you say “save a note blabla in MyApp” it won’t work. Don’t ask me why.
- If you call fetching function in onInit function, it won’t get invoked if you say “ok google…” while your app is in the foreground.
- There is no option to create your own voice actions
- If you download WeightTracker from Google Play and set it as default noting app, you should be able to use it with Google Assistant/Google Now. If not, please tell me!
If you would like to do it on your own, here are important materials: Voice Actions, Handling Intents.
If you are interested in full code, here are links to the repository, MainActivity class, onInit method, and note handling method. (I am using Redux architecture)
If you have any questions, feel free to ask!
Founder of Fidev
Flutter enthusiast since Alpha release in 2017. Believes that sharing is caring, which lead him to start a technical blog dedicated fo Flutter in its early days. Loves to see beautiful designs become real apps and is willing to help make it happen. Enjoys sunny beaches far from home.
Join the newsletter!
Join the newsletter to keep track with latest posts and get my special Widget to animate views' entrances without any hassle for FREE!
Check out other posts!
Stripe Checkout in Flutter Web
Flutter Web is getting more mature every day. If you want to accept payments using Stripe Checkout in your Flutter web application, this article is just for you!
Stripe Checkout in mobile Flutter app
Have you ever struggled to integrate card payments into your mobile Flutter app? If so, today is your lucky day! In this post, I present how to use Stripe Checkout in the Flutter app without any hassle!
Interacting with Widgets using Framy
Have you ever developed a widget or a page and you wanted to make sure it works correctly in different scenarios but then it turned out that you can’t just reproduce all the cases you want to cover? Framy may solve such problems!
czy jestes zainteresowany internship/ junior w UK jako software engineer. Jestem leadem Android Team w The App Business. Jednoczesnie jestem autorem Flutter publication na Medium.
Hi, thank you so much for sharing.
About the caveats you mentioned:
-“If you call fetching function in onInit function” –
well, if you have Java calling Flutter instead of the reverse, this will be resolved. I understand it’s possible. Was this an approach you tried? Did you find any issues with it?
-“There is no option to create your own voice actions” – is this a general restriction, or just using your method? I know Android offers custom actions, where’s the issue implementing these using Flutter?
-Do you see any issues trying to implement a 2-way conversation with the user? In your example you only allow a 1-way call from the user, no questions back…
Would you mind providing any sources on how those things might be achievable? 🙂
OK, we’ll learn together 🙂
1) Platform Channels seem to support the Java-to-Dart call direction.
See https://flutter.io/platform-channels/ , search for “can also be sent in the reverse direction”.
2&3) Starting API23, Android supports custom actions + 2-way conversations,
This is just the info, I haven’t tried anything yet 🙂 Have fun, and please, do share.
Seems Custom Voice Actions are not supported after all. Sorry.
Gracias por la informacion!! Este tipo de blogs me parecen muy importantes, esto discuto en mi casa de estudios, donde los voy a compartir gustosamente con mis compañeros. gracias por la informacion. https://uautonoma.cl
Que bueno ver gente de la misma ciudad interesada a este nivel en flutter.
Thanks to share content like this. This type of content literally help people to get knowledge about trending technology. At present time, most of the person use Google flutter for his iOS app development. I am also a iOS app development and using Google flutter. If you guys have any of query regarding Google Flutter then you can ask me via reply to this comment. Thank you.
Is there a way to alter the timeout for silence in the take a note. For some cases there may be longer pauses. It seems to decide too quickly that you are done talking for the note.
Is there some way to test this functionality without posting an app to Google play?