Skip to content

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

Closed
alishe79 opened this issue Nov 29, 2021 · 7 comments
Closed

how add button to initial notification ? #31

alishe79 opened this issue Nov 29, 2021 · 7 comments

Comments

@alishe79
Copy link

how add button named "Disconnect" in AndroidNotificationOptions ?

my simple code is here .

const AndroidNotificationOptions(
      channelId: 'parameter_channel',
      channelName: 'Parameters',
      channelDescription: 'This notification appears when the reading begaz params running.',
      channelImportance: NotificationChannelImportance.LOW,
      priority: NotificationPriority.LOW,
      visibility: NotificationVisibility.VISIBILITY_PRIVATE,
      playSound: false,
      enableVibration: false,
      iconData: NotificationIconData(
        resType: ResourceType.mipmap,
        resPrefix: ResourcePrefix.ic,
        name: 'launcher',
      ),
    ),
@Dev-hwang
Copy link
Owner

The feature to add a button to a notification is not implemented yet.

@Dev-hwang
Copy link
Owner

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.

@alishe79
Copy link
Author

Thanks for everything.

@Dev-hwang
Copy link
Owner

Dev-hwang commented Dec 2, 2021

@alishe79

Add the following line to your pubspec.yaml file.

dependency_overrides:
  flutter_foreground_task:
    git:
      url: https://github.com/Dev-hwang/flutter_foreground_task.git
      ref: add_notification_button

Run the command.

flutter clean
flutter pub get

Use the buttons option in the FlutterForegroundTask.init function 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.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 onButtonPressed function in the TaskHandler implementation class.

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');
  }
}

Result
image
image

I will do more testing and upgrade the plugin. First, try using the plugin by overriding the dependency.

@Dev-hwang
Copy link
Owner

The plugin has been updated. You can upgrade to version 3.5.0.

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.

image

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,
    );
  }

@alishe79
Copy link
Author

alishe79 commented Dec 5, 2021

This greate :) ! Thanks for work.

@alishe79 alishe79 closed this as completed Dec 5, 2021
@reverieline
Copy link

Hi! Is it posible to update button text while service is running?

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

No branches or pull requests

3 participants