0.1.45 • Published 12 months ago
foreground-ss v0.1.45
foreground-ss
React Native Expo foreground service
Install guide
npx expo install foreground-ssadd this script to the root of the expo project name withForegroundService.js
const { withAndroidManifest } = require('@expo/config-plugins');
const withForegroundService = (config) => {
return withAndroidManifest(config, (config) => {
const permissions = [
'android.permission.POST_NOTIFICATIONS',
'android.permission.FOREGROUND_SERVICE',
'android.permission.FOREGROUND_SERVICE_DATA_SYNC',
];
if (!config.modResults.manifest['uses-permission']) {
config.modResults.manifest['uses-permission'] = [];
}
permissions.forEach((permission) => {
const alreadyExists = config.modResults.manifest['uses-permission'].some(
(item) => item.$['android:name'] === permission
);
if (!alreadyExists) {
config.modResults.manifest['uses-permission'].push({
$: { 'android:name': permission },
});
}
});
const service = {
$: {
'android:name': 'expo.modules.foreground.ForegroundService',
'android:enabled': 'true',
'android:exported': 'true',
'android:foregroundServiceType': 'dataSync',
},
};
// Ensure the <application> tag exists and add the service
if (!config.modResults.manifest.application[0].service) {
config.modResults.manifest.application[0].service = [];
}
const hasService = config.modResults.manifest.application[0].service.some(
(existingService) =>
existingService.$['android:name'] === 'expo.modules.foreground.ForegroundService'
);
if (!hasService) {
config.modResults.manifest.application[0].service.push(service);
}
return config;
});
};
module.exports = withForegroundService;add config to app.json
"plugins": [..., "./withForegroundService.js"],Useage
npm run androidIn code to start a foreground service the app need
- Allow Post Notification permission
- Allow FOREGROUND permission
- Allow FOREGROUND_DATA_SYNC permisiion
Example useage
import * as ForegroundModule from "foreground-ss";
const requestNotificationPermission = async () => {
if (Platform.OS === "android") {
try {
PermissionsAndroid.check("android.permission.POST_NOTIFICATIONS")
.then((response) => {
if (!response) {
PermissionsAndroid.request(
"android.permission.POST_NOTIFICATIONS",
{
title: "Notification",
message:
"App needs access to your notification " +
"so you can get Updates",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK",
}
);
}
})
.catch((err) => {
console.log("Notification Error=====>", err);
});
} catch (err) {
console.log(err);
}
}
};
export default function Home() {
// request for foreground service permission
useEffect(() => {
requestNotificationPermission();
}, []);
const handlePress = () => {
ForegroundModule.default.startForegroundService(
// API end point return the data
"https://dc70-101-99-23-76.ngrok-free.app/data"
);
};
const handleStop = () => {
ForegroundModule.default.stopForegroundService();
};
return (
<>
<Stack.Screen options={{ title: "Home" }} />
<Container>
<ScreenContent path="app/index.tsx" title="Home" />
<Button title="Show Details" onPress={handlePress} />
<Button title="Stop Service" onPress={handleStop} />
</Container>
</>
);
}0.1.41
12 months ago
0.1.42
12 months ago
0.1.43
12 months ago
0.1.44
12 months ago
0.1.45
12 months ago
0.1.34
1 year ago
0.1.35
1 year ago
0.1.36
1 year ago
0.1.37
1 year ago
0.1.40
1 year ago
0.1.38
1 year ago
0.1.39
1 year ago
0.1.32
1 year ago
0.1.33
1 year ago
0.1.30
1 year ago
0.1.31
1 year ago
0.1.29
1 year ago
0.1.27
1 year ago
0.1.28
1 year ago
0.1.22
1 year ago
0.1.23
1 year ago
0.1.24
1 year ago
0.1.25
1 year ago
0.1.26
1 year ago
0.1.21
1 year ago
0.1.20
1 year ago
0.1.19
1 year ago
0.1.18
1 year ago
0.1.17
1 year ago
0.1.16
1 year ago
0.1.15
1 year ago
0.1.14
1 year ago
0.1.13
1 year ago
0.1.12
1 year ago
0.1.11
1 year ago
0.1.10
1 year ago
0.1.9
1 year ago
0.1.8
1 year ago
0.1.7
1 year ago
0.1.6
1 year ago
0.1.5
1 year ago
0.1.4
1 year ago
0.1.3
1 year ago
0.1.2
1 year ago
0.1.1
1 year ago
0.1.0
1 year ago