Skip to content

Commit

Permalink
fix: Added notification icon settings for Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 14, 2023
1 parent f8a4785 commit 0cb631a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
72 changes: 72 additions & 0 deletions packages/katana_cli/lib/action/firebase/messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
import 'dart:io';

// Package imports:
import 'package:image/image.dart';
import 'package:xml/xml.dart';

// Project imports:
import 'package:katana_cli/katana_cli.dart';

final _sizeListNotificationIcon = {
"android/app/src/main/res/mipmap-hdpi/ic_launcher_notification.png": 162,
"android/app/src/main/res/mipmap-mdpi/ic_launcher_notification.png": 108,
"android/app/src/main/res/mipmap-xhdpi/ic_launcher_notification.png": 216,
"android/app/src/main/res/mipmap-xxhdpi/ic_launcher_notification.png": 324,
"android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_notification.png": 432,
};

/// Initialize FirebaseMessaging settings.
///
/// FirebaseMessagingの初期設定を行います。
Expand Down Expand Up @@ -36,6 +45,7 @@ class FirebaseMessagingCliAction extends CliCommand with CliActionMixin {
final firebase = context.yaml.getAsMap("firebase");
final messaging = firebase.getAsMap("messaging");
final channelId = messaging.get("channel_id", "");
final notificationIcon = messaging.get("android_notification_icon", "");
if (channelId.isEmpty) {
error(
"Channel ID is not specified in [firebase]->[messaging]->[channel_id]. Please specify any ID.",
Expand Down Expand Up @@ -76,6 +86,42 @@ class FirebaseMessagingCliAction extends CliCommand with CliActionMixin {
"notificationChannelId=$channelId",
);
}
if (notificationIcon.isNotEmpty) {
label("Create notification icon.");
final iconFile = File(notificationIcon);
if (!iconFile.existsSync()) {
error("Icon file not found in $notificationIcon.");
return;
}
final iconImage = decodeImage(iconFile.readAsBytesSync())!;
if (iconImage.width != 1024 || iconImage.height != 1024) {
error("Icon files should be 1024 x 1024.");
return;
}
for (final tmp in _sizeListNotificationIcon.entries) {
label("Resize & Save to ${tmp.key}");
final dir = Directory(tmp.key.parentPath());
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
final file = File(tmp.key);
if (file.existsSync()) {
await file.delete();
}
final resized = adjustColor(
copyResize(
iconImage,
height: tmp.value,
width: tmp.value,
interpolation: Interpolation.average,
),
mids: ColorUint8.rgb(255, 255, 255),
blacks: ColorUint8.rgb(255, 255, 255),
whites: ColorUint8.rgb(255, 255, 255),
);
await file.writeAsBytes(encodePng(resized, level: 9));
}
}
label("Edit AndroidManifest.xml.");
final file = File("android/app/src/main/AndroidManifest.xml");
if (!file.existsSync()) {
Expand All @@ -91,6 +137,32 @@ class FirebaseMessagingCliAction extends CliCommand with CliActionMixin {
"The structure of AndroidManifest.xml is broken. Do `katana create` to complete the initial setup of the project.",
);
}
if (notificationIcon.isNotEmpty &&
!application.first.children.any((p0) =>
p0 is XmlElement &&
p0.name.toString() == "meta-data" &&
(p0.findElements("action").firstOrNull?.attributes.any((item) =>
item.name.toString() == "android:name" &&
item.value ==
"com.google.firebase.messaging.default_notification_icon") ??
false))) {
application.first.children.add(
XmlElement(
XmlName("meta-data"),
[
XmlAttribute(
XmlName("android:name"),
"com.google.firebase.messaging.default_notification_icon",
),
XmlAttribute(
XmlName("android:resource"),
"@mipmap/ic_launcher_notification",
),
],
[],
),
);
}
if (!activity.first.children.any((p0) =>
p0 is XmlElement &&
p0.name.toString() == "intent-filter" &&
Expand Down
3 changes: 3 additions & 0 deletions packages/katana_cli/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,14 @@ ${showAllConfig ? """
# Enable Firebase Messaging.
# Specify ChannelNotificationId for Android in [channel_id].
# Specify an image path in [android_notification_icon] to set a notification icon for Android for whiteout.
# Firebase Messagingを有効にします。
# [channel_id]にAndroid用のChannelNotificationIdを指定してください。
# [android_notification_icon]に画像パスを指定するとAndroid用の白抜き用の通知アイコンを設定できます。
messaging:
enable: false
channel_id:
android_notification_icon:
${showAllConfig ? """
Expand Down
4 changes: 2 additions & 2 deletions packages/katana_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ packages:
dependency: transitive
description:
name: uuid
sha256: df5a4d8f22ee4ccd77f8839ac7cb274ebc11ef9adcce8b92be14b797fe889921
sha256: bb55f38968b9427ce5dcdb8aaaa41049282195e0cfa4cf48593572fa3d1f36bc
url: "https://pub.dev"
source: hosted
version: "4.2.1"
version: "4.3.1"
web:
dependency: transitive
description:
Expand Down

0 comments on commit 0cb631a

Please sign in to comment.