Open deban07 opened 2 months ago
Same problem when i upgrade the targetSdkVersion to 34
I am already working on it. Please wait for the newer version.
On targetSdkVersion 34, you need to request for POST_NOTIFICATIONS before starting the service
import { Platform } from "react-native";
import Permissions from "react-native-permissions";
// You can use PermissionsAndroid
const startForegroundService = async () => {
if (Platform.Version >= 33) {
const response = await Permissions.request(
Permissions.PERMISSIONS.ANDROID.POST_NOTIFICATIONS
);
if (response !== Permissions.RESULTS.GRANTED) {
return;
}
}
// Start the service
};
in my case, i settled the android:foregroundServiceType
in AndroidManifest.xml
like this and it works:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<service android:name="com.supersami.foregroundservice.ForegroundService" android:foregroundServiceType="location"></service>
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask" android:foregroundServiceType="location"></service>
The value of android:foregroundServiceType
depends of you use case. More details here:
In my case, android:foregroundServiceType="microphone"
, I had to request for PermissionsAndroid.RECORD_AUDIO
before starting the service
@dukizwe this is a final solution ? im try to restart service for my company on phone with android 14 the foreground service not working..
@nmagliorua for me the solution i shared fixed the issue. you need to try it and see if it can also fix the issue
Just to add my findings, since this doesnt seem to be in the documentation yet:
you can now add a Servicetype
to the start
function, that will map the ServiceType to the right module that startForeground()
expects:
return ReactNativeForegroundService.start({
id: 123,
title: 'SomeTitle',
message: 'SomeMesssage',
icon: 'some_icon',
ServiceType: 'location'
});
Here is the mapping in ForegroundService.java
as of 2.2.1
:
switch (customServiceType) {
case "camera":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA;
case "connectedDevice":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE;
case "dataSync":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC;
case "health":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH;
case "location":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION;
case "mediaPlayback":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK;
case "mediaProjection":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
case "microphone":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
case "phoneCall":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL;
case "remoteMessaging":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING;
case "shortService":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE;
case "specialUse":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE;
case "systemExempted":
return ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED;
default:
throw new IllegalArgumentException("Unknown foreground service type: " + customServiceType);
}
The other declarations like here https://github.com/Raja0sama/rn-foreground-service/issues/98#issuecomment-2298691900 still apply.
Edit: Forgot to mention that i got the Service running as expected. Also the android dev documentation for service types and their requirements: https://developer.android.com/about/versions/14/changes/fgs-types-required
Every service type has some required permissions that we need. Please make sure you go through what service location peer permission we required.
@Raja0sama
Android: 14 React Native : 0.74.3
You might see the "Start Service Triggered" message, but no callback will occur because the task isn’t actually added.
Here's the full explanation:
The issue lies with add_task
, which gets removed when the app is closed, causing tasks to stop.
As a workaround, when a notification is received, I check if the task is already running using the is_task_running
API.
If it is, I proceed to start the task. If it isn’t, I reinitialize it by calling ReactNativeForegroundService.add_task(...)
and then send an immediate notification. When this new notification is received, the task is successfully added and continues to run, allowing the foreground task to start as expected.
I hope this helps clarify the workaround!
Background Service is not working on Android 14 , even after '' Allow all the time " manually . Service is not working in background and phone lock mode.