I'm testing siri shortcut features following README instructions.
donate shortcut and present shortcut work like a charm.
However, when I came in the app through the shortcut, SiriShortcutsEvent was never called.
Also, suggestShortcuts does not work either. I couldn't find shortcut when I only used suggestShortcuts.
This is my full code.
import React, {useState, Component} from 'react';
import {
SafeAreaView,
View,
Button,
StyleSheet,
Text,
} from 'react-native';
import {
SiriShortcutsEvent,
donateShortcut,
suggestShortcuts,
clearAllShortcuts,
clearShortcutsWithIdentifiers,
getShortcuts,
presentShortcut,
} from 'react-native-siri-shortcut';
import AddToSiriButton, {
SiriButtonStyles,
} from 'react-native-siri-shortcut/AddToSiriButton';
const opts = {
activityType: 'com.alt.reactnativetdd.test', // This activity type needs to be set in `NSUserActivityTypes` on the Info.plist
title: 'Say Hi',
userInfo: {
foo: 1,
bar: 'baz',
},
keywords: ['kek', 'foo', 'bar'],
persistentIdentifier: 'yourPersistentIdentifier',
isEligibleForSearch: true,
isEligibleForPrediction: true,
suggestedInvocationPhrase: 'Say something',
needsSave: true,
};
class App extends Component {
state = {
shortcuts: [],
};
componentDidMount() {
SiriShortcutsEvent.addListener(
'SiriShortcutListener',
({userInfo, activityType}) => {
// Do something with the userInfo and/or activityType
console.log('userInfo', userInfo);
console.log('activityType', activityType);
},
);
suggestShortcuts([opts]);
this.updateShortcutList();
}
async updateShortcutList() {
try {
const shortcuts = await getShortcuts();
this.setState({
shortcuts,
});
} catch (e) {
alert("You're not running iOS 12!");
}
}
render() {
const {shortcuts} = this.state;
return (
<SafeAreaView style={styles.container}>
<Button
title="Present Shortcut"
onPress={() => {
presentShortcut(opts, ({status}) => {
console.log('status', status);
});
}}
/>
<Button title="Donate Shortcut" onPress={() => donateShortcut(opts)} />
<Button
title="Clear Shortcuts With Identifiers"
onPress={async () => {
try {
await clearShortcutsWithIdentifiers([
'com.alt.reactnativetdd.test',
'some.identifier',
'another.identifier',
,
]);
// Shortcuts cleared
} catch (e) {
// Can't clear shortcuts on <iOS 12 because they don't exist
}
}}
/>
<Button
title="Clear All Shortcuts"
onPress={async () => {
try {
await clearAllShortcuts();
// Shortcuts were successfully cleared
} catch (e) {
// Can't clear shortcuts on <iOS 12 because they don't exist
}
}}
/>
<AddToSiriButton
style={{flex: 1}}
buttonStyle={SiriButtonStyles.whiteOutline}
onPress={() => {
console.log('You clicked me');
}}
/>
{shortcuts.length ? (
shortcuts.map(({identifier, phrase, options}, i) => (
<View key={identifier}>
<Text>Shortcut {i + 1}:</Text>
<Text>Identifier - {identifier}</Text>
<Text>Phrase - {phrase}</Text>
<Text>Options - {JSON.stringify(options)}</Text>
</View>
))
) : (
<Text>No Shortcuts yet</Text>
)}
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
contentContainer: {
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;