Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
react-native-device-activity
Advanced tools
Provides access to Apples DeviceActivity API. It does require a Custom Dev Client to work with Expo.
Please note that it only supports iOS (and requires iOS 15 or higher). For Android I'd probably look into UsageStats, which seems provide more granularity.
import * as ReactNativeDeviceActivity from "react-native-device-activity";
const DeviceActivityPicker = () => {
// First things first, you need to request authorization
useEffect(() => {
ReactNativeDeviceActivity.requestAuthorization()
}, [])
const [familyActivitySelection, setFamilyActivitySelection] = React.useState(null);
// next you need to present a native view to let the user select which activities to track, you need to do this before you can start tracking (this is a completely unstyled clickable native view):
return (
<ReactNativeDeviceActivity.DeviceActivitySelectionView
onSelectionChange={(event) => {
setFamilyActivitySelection(
event.nativeEvent.familyActivitySelection
)
}}
familyActivitySelection={familyActivitySelection}>
<Text>Click here</Text>
</ReactNativeDeviceActivity.DeviceActivitySelectionView>)
}
}
// once you have authorization and got hold of the familyActivitySelection (which is a base64 string) you can start tracking with it:
const trackDeviceActivity = (activitySelection: string) => {
ReactNativeDeviceActivity.startMonitoring(
"DeviceActivity.AppLoggedTimeDaily",
{
// repeat logging every 24 hours
intervalStart: { hour: 0, minute: 0, second: 0 },
intervalEnd: { hour: 23, minute: 59, second: 59 },
repeats: true,
},
events: [
{
eventName: 'user_activity_reached_10_minutes',
familyActivitySelection: activitySelection,
threshold: { minute: 10 },
}
]
);
}
// you can listen to events (which I guess only works when the app is alive):
const listener = ReactNativeDeviceActivity.addEventReceivedListener(
(event) => {
const name = event.nativeEvent.callbackName; // the name of the event
/* callbackName is one of, corresponding to the events received from the native API:
- "intervalDidStart"
- "intervalDidEnd"
- "eventDidReachThreshold"
- "intervalWillStartWarning"
- "intervalWillEndWarning"
- "eventWillReachThresholdWarning";
*/
}
);
// you can also get a history of events called with the time where called:
const events = ReactNativeDeviceActivityModule.getEvents();
For managed Expo projects, please follow the installation instructions in the API documentation for the latest stable release. If you follow the link and there is no documentation available then this library is not yet usable within managed projects — it is likely to be included in an upcoming Expo SDK release.
The package requires native code, which includes a custom app target. Currently it requires targeting iOS 15 or higher, so populate app.json/app.config.json as follows:
"plugins": [
[
"expo-build-properties",
{
"ios": {
"deploymentTarget": "15.0"
},
},
],
[
"react-native-device-activity",
{
"appleTeamId": "<YOUR_TEAM_ID>",
}
]
],
The Swift files for the iOS target will be copied to your local /targets
directory. You might want to add it to your .gitignore.
⚠️ Please note that you need to apply to apply to use this API before launching it in AppStore
For bare React Native projects, you must ensure that you have installed and configured the expo
package before continuing.
npm install react-native-device-activity
Run npx pod-install
after installing the npm package.
Contributions are very welcome! Please refer to guidelines described in the contributing guide.
FAQs
Provides access to Apples DeviceActivity API
The npm package react-native-device-activity receives a total of 4 weekly downloads. As such, react-native-device-activity popularity was classified as not popular.
We found that react-native-device-activity demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.