Can not start HeadlesTask
Description
I have an app that is targeted SDK 32. I want to send a headless task from my native android code when the app is killed but I can not on android version greater the 10. If I can not call a headless task from my code with targeted SDK 32 please help me to solve with other ways.
Version
0.68.1
Output of npx react-native info
System: OS: Windows 10 10.0.19044 CPU: (4) x64 Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz Memory: 446.03 MB / 3.95 GB Binaries: Node: 14.17.6 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD Watchman: 20220313.154008.0 - C:\watchman\bin\watchman.EXE SDKs: Android SDK: Not Found Windows SDK: AllowAllTrustedApps: Disabled IDEs: Android Studio: Version 2021.1.0.0 AI-211.7628.21.2111.8193401 Visual Studio: Not Found Languages: Java: 11.0.2 - /c/Program Files/Java/jdk-11.0.2/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.0.0 => 18.0.0 react-native: 0.68.1 => 0.68.1 react-native-windows: Not Found npmGlobalPackages: react-native: Not Found
Steps to reproduce
- Check if the app is foreground or not
- If app is not in foreground send headless task
I follow the ways from official documentation. Note: I want to call headless task from MainApplication.java class not inside of any Receiver.
Snack, code example, screenshot, or link to a repository
package com.your_application_name;
import android.content.Intent; import android.os.Bundle; import com.facebook.react.HeadlessJsTaskService; import com.facebook.react.bridge.Arguments; import com.facebook.react.jstasks.HeadlessJsTaskConfig; import javax.annotation.Nullable;
public class MyTaskService extends HeadlessJsTaskService {
@Override protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { return new HeadlessJsTaskConfig( "SomeTaskName", Arguments.fromBundle(extras), 5000, // timeout for the task false // optional: defines whether or not the task is allowed in foreground. Default is false ); } return null; } }
Intent service = new Intent(getApplicationContext(), MyTaskService.class); Bundle bundle = new Bundle();
bundle.putString("foo", "bar"); service.putExtras(bundle);
getApplicationContext().startService(service);
but I can not on android version greater the 10
Why you cannot?
but I can not on android version greater the 10
Why you cannot?
@cortinico I'm trying to call the headless task inside a worker. I'm checking first if SDK version is greater then Oreo then call context.startForegroundService else call context.startService. This is my code inside the worker class:
try{
Intent headlessService = new Intent(mContext, MyHeadlessTask.class);
Data data = getInputData();
Bundle serviceBundle = new Bundle();
serviceBundle.putString("unique_identifier",data.getString("unique_identifier"));
serviceBundle.putString("name",data.getString("name"));
headlessService.putExtras(serviceBundle);
Log.i("com.test.app","Started worker you could see the headless notification (API O+)");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mContext.startForegroundService(headlessService);
}else{
mContext.startService(headlessService);
}
HeadlessJsTaskService.acquireWakeLockNow(mContext);
return Result.success();
}catch (Exception exc){
Log.i(com.test.app","An error occured when calling doWork Headless: "+ exc.getMessage());
exc.printStackTrace();
return Result.failure();
}
on catch block I'm getting this error message:
An error occured when calling doWork Headless: startForegroundService() not allowed due to mAllowStartForeground false: service com.test.app/com.test.app.MyHeadlessTask
And this is my headlestTask class :
public class MyHeadlessTask extends HeadlessJsTaskService {
public MyHeadlessTask() {
}
@Nullable
@Override
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
String title = "";
String text = "";
int iconResource = -1;
Notification notification = new NotificationCompat.Builder(getApplicationContext(), "my_channel")
.setContentTitle(title)
.setContentText(text)
.setTimeoutAfter(1)
.setOngoing(true)
.build();
startForeground(1, notification);
}
Bundle extras = intent.getExtras();
if(extras != null){
Log.i("com.test.app","Extras is not null");
return new HeadlessJsTaskConfig(
"beaconRegionDidEnter",
Arguments.fromBundle(extras),
5000, // timeout for the task
true // optional: defines whether or not the task is allowed in foreground. Default is false
);
}
Log.i("com.test.app","Extras is null");
return null;
}
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
String description = "My channel description";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel("my_channel", "My service", importance);
channel.setDescription(description);
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
}
and it is registered on android manifest file like this
<service
android:name=".services.MyHeadlessTask"
android:enabled="true"
android:exported="true" />
Device that used for testing is SAMSUNG SM-A515F - Device API level 31 This code is running very well on android 10 devices
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been stalled for 7 days with no activity.