Skip to main content

Listening for Shortcuts

Whenever the user triggers a shortcut while your app was in the background, the listener will get called, allowing you to handle the shortcut request.

import * as React from "react";
import { addShortcutListener } from "react-native-siri-shortcut";

const App = () => {
React.useEffect(() => {
const subscription = addShortcutListener(({ userInfo, activityType }) => {
console.log(
`User requested ${activityType} be handled, with ${userInfo} context`
);
});

return () => {
subscription.remove();
};
}, []);

return null;
};

API Definition

addShortcutListener

Adds an event listener for triggered shortcuts.

Returns an object with remove() to remove this listener.

const subscription = addShortcutListener(callback: ShortcutListener);

subscription.remove();

getInitialShortcut

Get the shortcut that launched the app, if any.

Returns Promise<ShortcutInfo | null>

const initialShortcut = await getInitialShortcut();

if (initialShortcut) {
// Handle the shortcut that launched the app
}

Type Reference

ShortcutListener

type ShortcutListener = (shortcut: ShortcutInfo) => void;

ShortcutInfo

type ShortcutInfo = {
activityType: string;
userInfo?: { [key: string]: any };
}