-
Notifications
You must be signed in to change notification settings - Fork 124
Foreground service stops after few hours #13
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
Comments
The above problem seems to be related to the Android doze mode or not handling the exception that occurs inside the initDispatcher function. Try the following:
|
Still having issues? |
The issue is still there, after some time the steps just stops counting. I tried an interval update of 1 hour and checked but after few hours, the service still stops. |
I've been using this plugin with the pedometer plugin for 3 days and haven't had any issues. Still works fine in the background. I need more information to test.
|
flutter doctor:
Build Mode: Issue occurs in both Debug and Release |
I will test it according to the environment. What do you do when a pedometer event occurs? (For example, data storage in DB, network communication, etc.) |
Yes, DB store, I used Objext_Box for storing steps in the DB. |
Hmm... that's strange... I tested it in the similar environment and there is no problem. env: code: @Entity()
class StepCountData {
int? id;
int steps;
String timestamp;
StepCountData({this.id, required this.steps, required this.timestamp});
}
void startCallback() async {
final stepCountStream = Pedometer.stepCountStream;
StreamSubscription<StepCount>? streamSubscription;
Store? store;
Box? stepCountDataBox;
FlutterForegroundTask.initDispatcher((timestamp, sendPort) async {
if (streamSubscription != null) return;
streamSubscription = stepCountStream.listen((event) {
// 데이터 생성
final data = StepCountData(
steps: event.steps,
timestamp: event.timeStamp.toString());
// 데이터 삽입
final id = stepCountDataBox?.put(data);
print('data id: $id');
// 노티피케이션 업데이트
FlutterForegroundTask.update(
notificationText: 'your steps: ${event.steps}');
// Send data to the main isolate.
sendPort?.send(event);
});
store = await openStore();
stepCountDataBox = store?.box<StepCountData>();
}, onDestroy: (timestamp) async {
store?.close();
await streamSubscription?.cancel();
});
} Would you like to upgrade this plugin to 3.0.0? The code is more stable. @Entity()
class StepCountData {
int? id;
int steps;
String timestamp;
StepCountData({this.id, required this.steps, required this.timestamp});
}
void startCallback() =>
FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
class FirstTaskHandler implements TaskHandler {
late Store store;
late Box stepCountDataBox;
late StreamSubscription<StepCount> streamSubscription;
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
store = await openStore();
stepCountDataBox = store.box<StepCountData>();
final stepCountStream = Pedometer.stepCountStream;
streamSubscription = stepCountStream.listen((event) {
// 데이터 생성
final data = StepCountData(
steps: event.steps,
timestamp: event.timeStamp.toString());
// 데이터 삽입
final id = stepCountDataBox.put(data);
print('data id: $id');
// 노티피케이션 업데이트
FlutterForegroundTask.updateService(
notificationText: 'your steps: ${event.steps}');
// Send data to the main isolate.
sendPort?.send(event);
});
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
}
@override
Future<void> onDestroy(DateTime timestamp) async {
store.close();
await streamSubscription.cancel();
}
} And I will continue to monitor this issue. |
Currently, I'm trying to implement a step counter which is calculated by foreground service having pedometer stream. I have observed that after few hours the foreground service stops, although the notification is still showing. Is there an alternative approach for solving this issue?
The text was updated successfully, but these errors were encountered: