-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal Exception: java.lang.RuntimeException want to use foregroundServiceType="connectedDevice" #240
Comments
I can confirm this is a bug, |
It's fixed it was just we have to allow permission at the beginning only |
@FaizyQadri @Faizan-Hurekatek @rakshitbharat How did you solve this? What are the additional permissions needed in the beginning? App Crashes for me. |
U suggest have to add permission in service tag as well |
<manifest ... > |
App crashes we want to use foregroundServiceType="connectedDevice"
Unable to start service com.asterinet.react.bgactions.RNBackgroundActionsTask@243c415 with Intent { cmp=com.iwelhealth.cgmpal/com.asterinet.react.bgactions.RNBackgroundActionsTask (has extras) mCallingUid=10388 }: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.iwelhealth.cgmpal/com.asterinet.react.bgactions.RNBackgroundActionsTask
android.app.ActivityThread.handleServiceArgs
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import android.os.Bundle // here
class MainActivity : ReactActivity() {
/**
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null)
}
override fun getMainComponentName(): String = "iWelCGMPal"
/**
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
}
import React, {useEffect, useState} from 'react';
import {
StyleSheet,
SafeAreaView,
KeyboardAvoidingView,
Platform,
Alert,
Linking,
PermissionsAndroid,
} from 'react-native';
import {Navigation} from './navigation/Navigation';
import {StatusBar} from 'expo-status-bar';
import {NavigationContainer} from '@react-navigation/native';
import BackgroundService from 'react-native-background-actions';
import CGMDataService from './services/cgmService';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {PERMISSIONS, requestMultiple} from 'react-native-permissions';
import DeviceInfo from 'react-native-device-info';
// Define your background task function
const veryIntensiveTask = async () => {
const {getCGMData} = CGMDataService();
// While the background service is running
while (BackgroundService.isRunning()) {
console.log('Running background task!');
}
};
// Options for the background service
const options = {
taskName: 'iWelCGMPal',
taskTitle: 'iWelCGMPal',
taskDesc: 'CGM Glucose Data description',
taskIcon: {
name: 'ic_launcher_notification',
type: 'mipmap',
},
linkingURI: 'yourSchemeHere://chat/jane',
parameters: {
delay: 60000,
},
};
const AppEntry = () => {
// State for token
const [token, setToken] = useState(null);
const [userid, setUserid] = useState(null);
// Function to check AsyncStorage for token regularly
const checkHexArray = async () => {
const savedToken = await AsyncStorage.getItem('token');
const savedUserid = await AsyncStorage.getItem('userid');
if (savedToken !== token && savedUserid !== userid) {
setToken(savedToken);
setUserid(savedUserid); // Update state only if it has changed
console.log('Device ID (token) updated:', savedToken, savedUserid);
}
};
useEffect(() => {
// Fetch token on mount
checkHexArray();
}, []);
const requestPermissions = async () => {
try {
if (Platform.OS === 'android') {
const apiLevel = await DeviceInfo.getApiLevel();
};
useEffect(() => {
if (token) {
// Start background service when token is available
const startBackgroundService = async () => {
try {
const isGrant = await requestPermissions();
if (isGrant) {
if (!BackgroundService.isRunning()) {
await BackgroundService.start(veryIntensiveTask, options);
console.log('Background service started');
}
} else {
console.log('no permission granted ');
}
} catch (e) {
console.error('Error starting background service:', e);
}
};
}, [token, userid]); // Re-run when token changes
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
style={styles.root}>
);
};
export default AppEntry;
const styles = StyleSheet.create({
root: {
flex: 1,
},
});
"react": "18.2.0",
"react-native": "0.73.6",
"react-native-background-actions": "^4.0.1",
The text was updated successfully, but these errors were encountered: