-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flutter_local_notifications] Added ability to bind to ForegroundService #2677
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
base: master
Are you sure you want to change the base?
[flutter_local_notifications] Added ability to bind to ForegroundService #2677
Conversation
MaikuB
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.actionnamespace exists. This is to follow convention of having the strings behind a namespace where last segment hasactionlike other actions e.g.android.intent.action.<...>andhtc.com.intent.action.<...> - create a class within the
action(i.e.com.dexterous.flutterlocalnotifications.intent.actionnamespace) that holds public static strings forcom.dexterous.flutterlocalnotifications.action.FOREGROUND_SERVICE_STARTEDandcom.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 isFlutterLocalNotificationsPluginIntent. It'd be similar to how developers can referenceIntent.ACTION_VIEW_LOCUS. In this case, they can referenceFlutterLocalNotificationsPluginIntent.FOREGROUND_SERVICE_STARTEDandFlutterLocalNotificationsPluginIntent.FOREGROUND_SERVICE_STOPPED
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