検索
[Flutter/dart] local_notificationで通知タイトルを太字にする
- M.R

- 2020年12月23日
- 読了時間: 2分
概要
flutterのlocal_notificationというライブラリを使うと、簡単に通知を作ることができます。今回は、local_notificationにおいて通知タイトルを太字にする方法を紹介します。基本的な使い方は公式サイトをご参照ください。
方法
iosの場合
iosの場合はデフォルトで太字になります。
Androidの場合
Androidの場合は何もしないとタイトルと本文は同じ太さになります。
タイトルを太字にする方法は以下です。
AndroidNotificationDetailsのstyleInformation:プロパティにDefaultStyleInformation()を設定し(2)、htmlFormatTitleをtrueにします(1)。
var styleInformation=DefaultStyleInformation(true, true); //1
var androidChannelSpecifics = AndroidNotificationDetails(
"channel_id",
"channel_name",
"channel_description",
importance: Importance.max,
priority: Priority.high,
styleInformation: styleInformation, //2
);DefaultStyleInformationコンストラクタの1項目をtrueにすると本文、2項目をtrueにするとタイトルにhtml形式の書式を設定できるようになります。
default_style_information.dart
/// 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;
}次に、通知を作成するメソッドのtitleプロパティに以下のように書式を設定して渡します。
例としてshowWeeklyAtDayAndTime()ですが、他のメソッドでも同じです。
await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
id,
Platform.isAndroid? '<b>${title}</b>': title, //ここ!
content,
day,
time,
platformChannelSpecifics,
payload: payload,
);iosの場合は書式は適用されないためプラットフォームがandroidかどうかで場合分けします。
最後に
太字以外にも書式設定ができるんでしょうか?試していないので分かりませんが。






コメント