Skip to content

Conversation

@nathanielheitsch
Copy link

@nathanielheitsch nathanielheitsch commented Aug 26, 2025

This PR adds the necessary components to the ForegroundService components to allow developers to listen for a broadcast and bind to the ForegroundService when it is created. This will promote the activity and reduce the likelihood of it being frozen or killed due to resource constraints by the OS. Updated the README and example app to show how it can be integrated if chosen.

Tested with the example app on Android 9 & 15

Copy link
Owner

@MaikuB MaikuB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and apologies for delay on getting back on this. I left some comments on the PR with some requested/proposed changes. Let me know what you think and happy to discuss further

⚠️ For Android 8.0+, sounds and vibrations are associated with notification channels and can only be configured when they are first created. Showing/scheduling a notification will create a channel with the specified id if it doesn't exist already. If another notification specifies the same channel id but tries to specify another sound or vibration pattern then nothing occurs.
### Bind ForegroundService to your FlutterActivity
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table of contents is missing an update that allows for jumping to this section

### Bind ForegroundService to your FlutterActivity
In your activity (e.g., `MainActivity.kt`), set up a broadcast receiver and a `ServiceConnection` to manage binding and unbinding. This is not required to use a `ForegroundService` but it will decrease the likelyhood of your activity being [killed or frozen by the OS while the activity is in the background](https://source.android.com/docs/core/perf/cached-apps-freezer#handling-custom-features):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this start off by saying it's optional and only applicable for apps using foreground services? Thinking behind this is some readers may not pick this up or know enough about Android development to realise

Intent intent = new Intent(applicationContext, ForegroundService.class);
intent.putExtra(ForegroundServiceStartParameter.EXTRA, parameter);
ContextCompat.startForegroundService(applicationContext, intent);
applicationContext.sendBroadcast(new Intent("com.dexterous.flutterlocalnotifications.FOREGROUND_SERVICE_STARTED")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you think but I believe it's best to do the following

  • add additional folders that so that com.dexterous.flutterlocalnotifications.intent.action namespace exists. This is to follow convention of having the strings behind a namespace where last segment has action like other actions e.g. android.intent.action.<...> and htc.com.intent.action.<...>
  • create a class within the action (i.e. com.dexterous.flutterlocalnotifications.intent.action namespace) that holds public static strings for com.dexterous.flutterlocalnotifications.action.FOREGROUND_SERVICE_STARTED and com.dexterous.flutterlocalnotifications.action.FOREGROUND_SERVICE_STOPPED. This to allow developers an easier way to reference the strings through the plugin. The best name I can come up with for the class is FlutterLocalNotificationsPluginIntent. It'd be similar to how developers can reference Intent.ACTION_VIEW_LOCUS . In this case, they can reference FlutterLocalNotificationsPluginIntent.FOREGROUND_SERVICE_STARTED and FlutterLocalNotificationsPluginIntent.FOREGROUND_SERVICE_STOPPED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants