Skip to content
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

Android: Foreground Service works on Foreground but not on Background #632

Closed
matheusyuri7 opened this issue Jan 2, 2023 · 11 comments
Closed
Labels
foreground-service needs-feedback Waiting on response to questions

Comments

@matheusyuri7
Copy link

matheusyuri7 commented Jan 2, 2023

Please somebody save me!!

I'm trying to run a background service to get the user's location.
I tried to follow the notifee's documentation and the service works in foreground, but it doesn't when the app is in background

Note: I'm using function and hooks

**notifee.registerForegroundService((notification) => {
console.log("notification ", notification);
return new Promise(() => {
// Long running task...
myLoop();
function myLoop() {
var i =0;
setTimeout(function() {
console.log('hello ', i);
i++;
if (i < 10) {
myLoop();
Geolocation.getCurrentPosition(
(position) => {
console.log("position ",position);
},
(error) => {
// See error code charts below.
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
}
}, 2000)
}
});
});

async function onDisplayNotification() {

  notifee.stopForegroundService();
  // Request permissions (required for iOS)
  await notifee.requestPermission();

  // Create a channel (required for Android)
  const channelId = await notifee.createChannel({
    id: 'default',
    name: 'Default Channel',
  });

  // Display a notification
  await notifee.displayNotification({
    title: 'Fitness Challenge',
    subtitle: 'Walk/Run',
    body:
      '0.02 km / 3 kcal',
    android: {
      channelId,
      asForegroundService: true,
      color: '#9A1E15',
      smallIcon: 'ic_notifications',
      actions: [
        {
          title: 'Pause',
          pressAction: { id: 'pause' },
        },
      ],
    },
  });
}**

Note: I already added the line below in Android.Manifest but it still doens't work

**<service android:name="app.notifee.core.ForegroundService" android:foregroundServiceType="location|camera|microphone" />**

@helenaford @mikehardy @liamjones @meypod

@meypod
Copy link
Contributor

meypod commented Jan 2, 2023

please stop mentioning everybody 😅I don't have a role here
I get you are frustrated but that's not a good way to get attention
but anyway, please format your code properly
and please mention what do you expect to happen vs what is happening, so maintainers can help you when they see this

@matheusyuri7
Copy link
Author

matheusyuri7 commented Jan 2, 2023

Sorry @meypod. I'm new in this stuff and don't know exactly how it works yet. Hope you have a wonderful new year

@matheusyuri7
Copy link
Author

I closed this issue by accident, but I will appreciate a lot if somebody helps me :)

@helenaford
Copy link
Member

have you searched the issues for anything similar? What happens when the app is in the background? you say it doesn't work, can you describe in more detail?

@helenaford helenaford added the needs-feedback Waiting on response to questions label Jan 3, 2023
@helenaford helenaford changed the title Foreground Service works on Foreground but not on Background Android: Foreground Service works on Foreground but not on Background Jan 3, 2023
@medonagy45
Copy link

medonagy45 commented Jan 5, 2023

Hi ,
I am facing the same issue ,the logs stops once the application loses focus and that is the log from logcat

D/DecorView[]: onWindowFocusChanged hasWindowFocus true
E/libprocessgroup: set_timerslack_ns write failed: Operation not permitted

the application shows the notification but the code doesn't run no logs nothing runs till it is back to foreground

@medonagy45
Copy link

I found out that the foreground service runs find and the issue is with setinterval I have used this package https://github.com/ocetnik/react-native-background-timer and it works fine now

@helenaford
Copy link
Member

@medonagy45 thanks for sharing your solution. Will close as it seems that issue is outside of notifee.

@AftabUfaq
Copy link

AftabUfaq commented Mar 6, 2024

i have faced this issue ona android 14

 java.lang.RuntimeException: Unable to start service app.notifee.core.ForegroundService@6b78ec8 with Intent { act=app.notifee.core.ForegroundService.START cmp=com.mobile.number.locator.phone.caller.location.finder/app.notifee.core.ForegroundService (has extras) }: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{e598b92 22242:com.mobile.number.locator.phone.caller.location.finder/u0a494} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION]  and the app must be in the eligible state/exemptions to access the foreground only permission
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5063)
    at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2427)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:224)
    at android.os.Looper.loop(Looper.java:318)
    at android.app.ActivityThread.main(ActivityThread.java:8720)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Caused by: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{e598b92 22242:com.mobile.number.locator.phone.caller.location.finder/u0a494} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION]  and the app must be in the eligible state/exemptions to access the foreground only permission
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7270)
    at android.app.Service.startForeground(Service.java:775)
    at app.notifee.core.ForegroundService.onStartCommand(SourceFile:17)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5045)
    at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2427) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:224) 
    at android.os.Looper.loop(Looper.java:318) 
    at android.app.ActivityThread.main(ActivityThread.java:8720) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013) 
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2804)
    at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2515)
    at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1806)
    at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:14009)
    at android.app.IActivityManager$Stub.onTransact$setServiceForeground$(IActivityManager.java:12679)

i fixed it with adding the following in the AndroidManifest.xml

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

@AftabUfaq
Copy link

FOREGROUND_SERVICE_LOCATION

@medonagy45 thanks for sharing your solution. Will close as it seems that issue is outside of notifee.

this is an issue with the permission on android 14 . and i have share the solution for this

@muhammadasifdotai
Copy link

Issue: Foreground Service Works in Foreground but Not in Background on Android

@mikehardy
Copy link
Collaborator

@muhammadasifdotai your comment does not have enough information to be actionable and is posted on an issue that has been closed for more than a year, meaning there will be no support on this issue

web search: stackoverflow how to ask
Also note perhaps #1078 related to new architecture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
foreground-service needs-feedback Waiting on response to questions
Projects
None yet
Development

No branches or pull requests

7 participants