-
Notifications
You must be signed in to change notification settings - Fork 124
how add button to initial notification ? #31
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 feature to add a button to a notification is not implemented yet. |
I checked the Android official documentation, but there are many difficulties in adding button functions to this plugin. This is because Dart code cannot be guaranteed to work normally even in the background, and accessibility with TaskHandler must also be considered. Let's try implementing it by creating a new branch. |
Thanks for everything. |
Add the following line to your dependency_overrides:
flutter_foreground_task:
git:
url: https://github.com/Dev-hwang/flutter_foreground_task.git
ref: add_notification_button Run the command.
Use the Future<void> _initForegroundTask() async {
await FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'notification_channel_id',
channelName: 'Foreground Notification',
channelDescription: 'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'sendButton', text: 'Send'),
const NotificationButton(id: 'testButton', text: 'Test'),
],
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: true,
playSound: false,
),
foregroundTaskOptions: const ForegroundTaskOptions(
interval: 5000,
autoRunOnBoot: true,
allowWifiLock: true,
),
printDevLog: true,
);
} And override the class FirstTaskHandler extends TaskHandler {
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
}
@override
Future<void> onDestroy(DateTime timestamp) async {
}
@override
void onButtonPressed(String id) {
// Called when the notification button on the Android platform is pressed.
print('onButtonPressed >> $id');
}
} I will do more testing and upgrade the plugin. First, try using the plugin by overriding the dependency. |
The plugin has been updated. You can upgrade to version dependencies:
flutter_foreground_task: ^3.5.0 In Android 11 and lower versions, the notification action button may not be displayed properly. This is because the notification importance is set LOW, so the notification is not in an expanded state. Therefore, the user must extend the notification or set the notification importance to HIGH as shown below. Future<void> _initForegroundTask() async {
await FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'notification_channel_id',
channelName: 'Foreground Notification',
channelDescription: 'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.HIGH, // here
priority: NotificationPriority.HIGH, // here
buttons: [
const NotificationButton(id: 'sendButton', text: 'Send'),
const NotificationButton(id: 'testButton', text: 'Test'),
],
),
iosNotificationOptions: const IOSNotificationOptions(),
foregroundTaskOptions: const ForegroundTaskOptions(),
printDevLog: true,
);
} |
This greate :) ! Thanks for work. |
Hi! Is it posible to update button text while service is running? |
how add button named "Disconnect" in AndroidNotificationOptions ?
my simple code is here .
The text was updated successfully, but these errors were encountered: