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:
<activity android:name=".MainActivity" | |
android:launchMode="singleTop" | |
android:theme="@android:style/Theme.Black.NoTitleBar" | |
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection" | |
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> | |
<action android:name="com.google.android.gms.actions.CREATE_NOTE"/> | |
<category android:name="android.intent.category.DEFAULT"/> | |
<data android:mimeType="text/plain"/> | |
</intent-filter> | |
</activity> |
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:
public class MainActivity extends FlutterActivity { | |
private String savedNote; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
Intent intent = getIntent(); | |
String action = intent.getAction(); | |
String type = intent.getType(); | |
if (NoteIntents.ACTION_CREATE_NOTE.equals(action) && type != null) { | |
if ("text/plain".equals(type)) { | |
savedNote = intent.getStringExtra(Intent.EXTRA_TEXT) | |
} | |
} | |
} | |
} |
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:
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
... | |
new MethodChannel(getFlutterView(), "app.channel.shared.data") | |
.setMethodCallHandler(new MethodChannel.MethodCallHandler() { | |
@Override | |
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { | |
if (methodCall.method.contentEquals("getSavedNote")) { | |
result.success(savedNote); | |
savedNote = null; | |
} | |
} | |
}); | |
} |
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:
Future<double> _getSavedWeightNote() async { | |
String sharedData = await const MethodChannel('app.channel.shared.data') | |
.invokeMethod("getSavedNote"); | |
if (sharedData != null) { | |
int firstIndex = sharedData.indexOf(new RegExp("[0-9]")); | |
int lastIndex = sharedData.lastIndexOf(new RegExp("[0-9]")); | |
if (firstIndex != -1) { | |
String number = sharedData.substring(firstIndex, lastIndex + 1); | |
double num = double.parse(number, (error) => null); | |
return num; | |
} | |
} | |
return null; | |
} |
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).