michael-cheung-thebus / foreground_service

Other
43 stars 29 forks source link

print() or log is not called. #21

Open Sandeep-GitAcc opened 4 years ago

Sandeep-GitAcc commented 4 years ago

I have following methods defined in a dart file:

void foregroundServiceFunction() {
  fireNotification();
  log.d("(foregroundServiceFunction) Service running");
}

Future<void> fireNotification() async {
  await ForegroundService.notification.startEditMode();
  await ForegroundService.notification.setTitle(NOTIICATION_SYNC);
  await ForegroundService.notification
      .setText("The time was: ${DateTime.now()}");
  await ForegroundService.notification
      .setPriority(AndroidNotificationPriority.DEFAULT);
  await ForegroundService.notification.finishEditMode();
}

Following log

log.d("(foregroundServiceFunction) Service running");

was never written.

I can also see some logs

D/QueuedWork(21061): waited: [<1: 0, <2: 77103, <4: 585, <8: 108, <16: 23, <32: 5, <64: 0, <128: 0, <256: 0, <512: 0, <1024: 0, <2048: 0, <4096: 0, <8192: 0, <16384: 0, >=16384: 0]

being printed continuously.

  1. Why logs were never printed? Is the service not running? When I call kill the app, my console disconnects so I couldn't monitor if the logs are being printed in background.
  2. On first launch I can see the notification I set before calling startForegroundService(), but after the schedule time notification doesn't change. If I kill the app and start again, notification set in the method is displayed. Why this behaviour?
  3. What are these extra logs that are being printed?

Some queries

  1. In function fireNotification(), Is it necessary to start edit mode before making change to notification being displayed?
  2. Is it possible to pass event to UI on tap of notification being displayed? How to configure onTap for the notification?
  3. The notification currently configured is inbuilt to _foregroundservice and I couldn't find any method in ForegroundService.notification class to configure custom notification or media notification with _foregroundservice. Is it possible?
michael-cheung-thebus commented 4 years ago

From your example and questions, I think it would be helpful to know your use-case. This plugin is focused around the service - in other words, executing dart code continuously at set intervals.

If notifications are your focus, try instead: https://pub.dev/packages/flutter_local_notifications

Sandeep-GitAcc commented 4 years ago

Use case:

  1. I want to run a polling service at a set interval. Even if app is killed
  2. Run scan over data received.
  3. Based on some conditions decides show notifications or not .

Above polling + scan won't take longer than 30 seconds.

Example: Whatsapp run a sync-up process at some interval and show notification if we have any new message. So here sync-up process is similar to running _foregroundservice at set interval. And showing notification is similar to using _flutter_localnotification

I have a different code in fireNotification() method (polling, scanning payload), here I have put an example code to reproduce the error. The method fireNotification() would have been called and notification would be updated. But this is not the case here.

1 . Why logs were never printed? Is the service not running? When I call kill the app, my console disconnects so I couldn't monitor if the logs are being printed in background.

  1. On first launch I can see the notification I set before calling startForegroundService(), but after the schedule time notification doesn't change. If I kill the app and start again, notification set in the method is displayed. Why this behaviour?
    1. What are these extra logs that are being printed?

These questions are mostly related why the service is behaving the way it is. I couldn't find nothing related to these queries. Can you please explain them a bit? May be document the same. Also I would like to add one more unclear functionality,

My other 3 queries were mostly related to notification which now I am trying to accommodate under _flutter_localnotification.

michael-cheung-thebus commented 4 years ago

Almost definitely android_alarm_manager is a better choice (in combination with flutter_local_notification) for your use case.

To answer your questions, though:

  1. If the notification shows up, the service is definitely running. The service is an Android thing, though - whether the Dart code is being run is a different deal. The full text of your main.dart would probably help with looking at that.

  2. If the notification is there, that means the app/service is already running. There are some things to get used to with flutter debugging - usually it works fine but there can be strange things, especially with hot reload/restart. Qutting the Flutter debug process entirely and redoing flutter run is what I've found to be the most reliable when debugging, but if it doesn't work try completely uninstalling the app and doing flutter clean before trying flutter run again.

  3. Those messages come from Flutter, not this plugin - likely debug info related to performance, since Flutter is built around UI/high performance.