Send Message from Application using SIRI

Author - Krishna Baldha

Siri is Apple’s personal virtual assistant which allows you to interact with your device via voice. SiriKit supports certain domains like VoIP calling, Messaging, Car Commands, Payments, Ride Booking, and Workouts. Within each domain, there is a series of intents, which are actions Siri can perform. For example, in Messaging domain, there are intents like sending a message, setting attributes on a message and searching for messages. In this blog, we are going to send a message without opening the app using Siri.

Let’s get started

First, create a simple view application.

Adding an intent extension to Xcode Project

Now go to File > New > Target . Then select Intents Extension from the list.

After pressing Next button, Enter the name of the extension and then click finish button.

After the finish button is pressed, you will also see the following popup. This popup indicates you to activate the newly added MessageaIntent scheme to use build and debug. So Activate this scheme.

After activating above scheme, you will see the following popup for asking the same activation of scheme MessageIntentUI. So again activate this scheme too.

You will see MessageIntent and MessageIntentUI in file navigator. MessageIntent extension handles all Siri features and MessageIntentUI extension use for displaying user interface.

Enable Siri and App Groups

Go to the Capabilities of main app and enable Siri as follow

Enable App Groups in all three targets as follow.

If there is no group id is displayed after enabling App Groups, you can manually add from Entitlements file.

In Info.plist file of MessageIntent Extension, you can see NSExtensionAttributes under NSExtension. Here three Intents are available.
INSendMessageIntent – use for sending messages
INSearchForMessagesIntent – use for searching messages
INSetMessageAttributeIntent – use for setting message content attributes.

You can edit the IntentsSupported as per your requirements.

IntentHandler File

You can see the INSendMessageIntentHandling, INSearchForMessagesIntentHandling, INSetMessageAttributeIntentHandling protocol and its method implemented in IntentHandler file.  In this demo, we will implement send message feature so here we need only INSendMessageIntentHandling. so you can remove the other two and its methods.

We are going to make an app in which first Siri will ask about the contact to whom we want to send a message. After that Siri will ask about the message which we want to send. And then after approval, a message will be sent.

First of all, we need to access Contact for the searching name in our contact list.

Add above key in Info.plist file. Now write following method for search contact from name.

In this method, recipients array are passed as arguments and we search with contact name from our contact list. And return final contact list array.Now we will call this method in the below method for getting the correct recipient.

Resolve Content Method

This method will be called when you speak your contact name. As per the above method, we search for the matching contact and depend on the matches found Siri will perform an action as per our requirement.
-> If there are more than two matches found then Siri will ask about which contact you want to send a message.
-> If there is one match found then Siri will ask about what message you want to send.
-> Also if there is no match found then Siri will again ask about the contact you want to send a message.
When you speak message then following method will be called.

If the text is empty then you need to ask about the text. And if there is a valid text then success will be called. Once a resolution is completed, perform validation on the intent confirmation in the following method. This is an optional method.

After all this, Siri will ask to send a message. If you say Yes then below method will be called and you have to handle data in this method.

This is a required method. A dictionary is created for storing message and recipient data in UserDefaults so that we can access this data in our main app through app groups. Now we are done with IntentHandler file and will create the UI for message display.

You can create your message UI as per your requirement in MainInterface Storyboard of MessageIntentUI folder. Here I have created a simple message display view with label outlet.

IntentViewController File

Below method will be called when Siri asked about the message and display the final message. so set your label’s value in this method.

Now only message and recipient name fetch and display are remaining in our main app. For that, you need to get data from extension to your app. When you open the app, applicationDidBecomeActive method will be called in Appdelegate. So write the code as below to notify the view controller about the latest message dictionary.

ViewController File

Take outlet of a message and recipient name label for displaying value. Then add and remove observer as below.

Write a function for setting data.

Now run your project with SiriMessageDemo target selected.

Then stop the app and select MessageIntent as a target.

After pressing the run button following popup will be displayed

Now choose your SiriMessageDemo app for run.

The following screen will be opened after some seconds or you can also open Siri by long pressing home button.

Now command Siri by saying something like “Send message using SiriMessageDemo” and the following screen will be opened.

Then speak the contact name which you want to send a message. Then contact permission alert will be opened and press OK.

After this, Siri will ask about a message.

Speak the message which you want to send. Then Siri will display the message in UI which you provide in MessageIntentUI.

You can either press send button or speak YES. Siri will send the message.

Now open the application. You can see the following screen with setting Recipient name and last message data.

Here we have displayed data very simply. You can set as per your requirement.

Don’t miss the next post!

Related Posts