skip to Main Content

I started Flutter exactly today.

I want to use HTML code within Android notifications.

It seems that by setting the DefaultStyleInformation argument to true, HTML can be used, but I don’t know how to write the actual code.

//This is the interface provided.

/// The default Android notification style.
class DefaultStyleInformation implements StyleInformation {
  /// Constructs an instance of [DefaultStyleInformation].
  const DefaultStyleInformation(
    this.htmlFormatContent,
    this.htmlFormatTitle,
  );

  /// Specifies if formatting should be applied to the content through HTML
  /// markup.
  final bool htmlFormatContent;

  /// Specifies if formatting should be applied to the title through HTML
  /// markup.
  final bool htmlFormatTitle;
}

The following is the code I am writing.
I feel I need help with the "//here" part.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:io'; 

 Future<void> showBigTextNotification() async {
    const styleInformation=DefaultStyleInformation(true,true);
    const NotificationDetails notificationDetails = 
    NotificationDetails(
      android: AndroidNotificationDetails(
       'channel_id',
       'Channel Name',
       importance: Importance.max,
       priority: Priority.high,
       styleInformation: styleInformation
       ),
       iOS: IOSNotificationDetails());

    await flutterLocalNotificationsPlugin.show(
      id, 
      Platform.isAndroid? '<b>'${title}'</b>': title,     // here???
      Platform.isAndroid? '<b>'${content}'</b>': content, // here???
      payload: 'Destination Screen(Big Text Notification)');
  }

thanks.

2

Answers


  1. you problem is in the bigTextInformation style and some more thing are missing please use the below code which will now convert html text into regular text

        Future<void> showBigTextNotification() async {
            BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
              body, htmlFormatBigText: true,
              htmlFormatTitle:true ,
              htmlFormatContent:true ,
                    contentTitle: 'overridden <b>big</b> content title',
                    htmlFormatContentTitle: true,
                    summaryText: 'summary <i>text</i>',
                    htmlFormatSummaryText: true
        
            );
      final androidChannelSpecifics = AndroidNotificationDetails(
        'your channel id',
        'your channel name',
        importance: Importance.max,
        styleInformation: bigTextStyleInformation,
        priority: Priority.high,
        ongoing: true,
        autoCancel: true,
      );
     final iOSChannelSpecifics = IOSNotificationDetails();
    
       NotificationDetails platformChannelSpecifics =  NotificationDetails(android: androidChannelSpecifics, iOS: iOSChannelSpecifics);
        
            await flutterLocalNotificationsPlugin.show(
              id, 
              Platform.isAndroid? '<b>'${title}'</b>': title,     // here???
              Platform.isAndroid? '<b>'${content}'</b>': content, // here???
              type: platformChannelSpecifics,
              payload: 'Destination Screen(Big Text Notification)');
          }
    
    Login or Signup to reply.
  2. Simply use BigTextStyleInformation to show html content in notifications

    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      '',
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channelDescription: channel.description,
          importance: Importance.high,
          styleInformation: BigTextStyleInformation(
            '<b>Your</b> notification',
            htmlFormatBigText: true,
          ),
        ),
      ),
      payload: jsonEncode(message.data),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search