MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.44k stars 1.39k forks source link

How can I change the title of an action in iOS? #2062

Closed dayron9110 closed 10 months ago

dayron9110 commented 1 year ago

Is it possible to change the title of an iOS action when showing a notification? That is after starting the flluter_local_notifications plugin? I want to receive the title of each action through the notification payload.

  Future<void> _setupLocalNotifications() async {
    _notificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(_androidNotificationChannel);
    const androidSettings = AndroidInitializationSettings('@drawable/ic_notification');
    final DarwinInitializationSettings iOSSettings = DarwinInitializationSettings(
      notificationCategories: [
        DarwinNotificationCategory(
          'mainCategory',
          actions: [
            DarwinNotificationAction.plain('id_1', 'Continuar', options: {DarwinNotificationActionOption.foreground}),
            DarwinNotificationAction.plain('id_2', 'Cancelar', options: {DarwinNotificationActionOption.foreground}),
          ],
          options: <DarwinNotificationCategoryOption>{
            DarwinNotificationCategoryOption.hiddenPreviewShowTitle,
          },
        ),
      ],
    );
    final initializationSettings = InitializationSettings(android: androidSettings, iOS: iOSSettings);
    await _notificationsPlugin.initialize(initializationSettings);
  }
  //Listening to the foreground messages
  Future<void> _onMessageHandler(RemoteMessage message) async {
    final notification = message.notification;
    final androidNotification = message.notification?.android;
    if (notification != null) {
      _notificationsPlugin.show(
        id++,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            _androidNotificationChannel.id,
            _androidNotificationChannel.name,
            channelDescription: _androidNotificationChannel.description,
            icon: androidNotification?.smallIcon,
            priority: Priority.high,
            styleInformation: const BigTextStyleInformation(''),
            actions: [
              const AndroidNotificationAction('id_1', 'Continuar', showsUserInterface: true),
              const AndroidNotificationAction('id_2', 'Cancelar', showsUserInterface: true),
            ],
          ),
          iOS: const DarwinNotificationDetails(categoryIdentifier: 'mainCategory'),
        ),
      );
    }
  }
MaikuB commented 1 year ago

Not sure this is possible and if the native platforms support this. If you need something like this then you'll need to investigate the platform capabilities further and if it is, you can contribute back the plugin to add the capability. Support for actions was a community contribution and if you look at Apple's docs, actions on iOS/macOS are tied to categories. Currently specifying the categories is tied to initialisation so perhaps you could do it be initialising the plugin or look at adding a separate method to the plugin to set the notification categories