Add shortcuts for Siri and the app icon
A shortcut is something your app does that the person can ask for without opening the app first: "Hey Siri, refresh Postcards", or a long press on the app's icon and a tap on "Show stamps". You list them in angular.json, and ng build --platform builds them into the app:
- On iOS, each one is an App Shortcut. Siri runs it when it hears one of its phrases, and it's listed in Spotlight and in the Shortcuts app, where the person can put it on the Action button or in an automation. iOS 16 and later.
- On Android, each one is in the menu a long press on the app's icon opens, from where it can also be dragged onto the home screen.
Either way the phone brings your app to the front, starting it if it has to, and then tells it which shortcut ran.
The preview has no Siri and no app icon, so the shortcuts only run in the app ng build --platform or ng serve --platform makes. See Try it.
List the shortcuts in angular.json
Add shortcuts to the build target's options:
Only id and title are required. iOS takes up to 10 shortcuts from an app, and Android's launchers show about four, the first ones listed, so put the ones people use most first.
Siri only listens for phrases that name the app, which is why every phrase needs {appName}. Siri hears the name under the app's icon (appName), so "Refresh {appName}" is said "Refresh Postcards". Write a few phrases for the ways people would say it: Siri matches what it hears against them, and doesn't guess at others.
Open a screen: give the shortcut a route
A shortcut with a route opens the app on that screen. provideRouter() from @blotch/angular-lynx navigates there when the shortcut runs, so it takes no code:
It works whether the app was open or closed. From closed, the app starts and goes straight to the route.
Do something: handle the shortcut
For anything else, such as loading the feed again, handle the shortcut's id with LynxShortcuts:
A handler registered in a constructor goes with its component: when the component is destroyed, the handler is removed. on() also returns a function that removes it sooner.
A shortcut can run before its handler exists: the shortcut that started the app runs before the app has drawn anything, and a handler may belong to a screen the person isn't on. So a shortcut that nothing handled waits for the first handler registered for its id. In the example above, "Refresh postcards" run from another screen reloads the postcards when the person next comes to this one. To handle a shortcut wherever the person is, register its handler in the root component or in a service the app creates when it starts.
A shortcut can have a route and a handler: the router is told to navigate first, then the handlers are called.
Try it
Shortcuts need the real app, so run it with --platform:
- On iOS, open the Shortcuts app. Your app is listed under Apps in its library, with a tile per shortcut: tap one to run it. On an iPhone, also try asking Siri. The iOS Simulator has the Shortcuts app and Spotlight.
- On Android, hold the app's icon on the home screen or in the app list, and tap a shortcut in the menu.
After changing shortcuts in angular.json, build the app again: they're compiled into it.
In the web preview, Lynx Explorer and tests
- In the web preview and in Lynx Explorer, no shortcut ever runs, and
on()does nothing. Give the app another way to do the same thing, as you would anyway: a shortcut is a second way in, not the only one. - In unit tests, run a shortcut with the testing library's
runShortcut(), and check what the app did:
What a shortcut can't do yet
- Take what the person said as input. "Show stamps from Japan" can't pass "Japan" to the app. Each shortcut is one fixed action, so list one per thing to open.
- Run without opening the app. Every shortcut brings the app to the front.
- Be said in another language. The phrases are in the one language you write them in.
- Be asked for by voice on Android. Google's assistant has no stable way for an app to declare its own phrases, so on Android the shortcuts are in the icon's menu only.
