Skip to content

Commit

Permalink
fix: Add logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Aug 25, 2023
1 parent 9e881aa commit 6b7e70e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/masamune_notification_firebase/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ packages:
path: "../../katana_router"
relative: true
source: path
version: "2.0.32"
version: "2.0.33"
katana_router_annotation:
dependency: "direct overridden"
description:
Expand Down Expand Up @@ -679,7 +679,7 @@ packages:
path: "../../masamune"
relative: true
source: path
version: "2.4.39"
version: "2.4.40"
masamune_annotation:
dependency: "direct overridden"
description:
Expand All @@ -693,7 +693,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.1.92"
version: "2.1.94"
matcher:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PushNotificationMasamuneAdapter extends MasamuneAdapter {
this.windowsOptions,
this.macosOptions,
this.linuxOptions,
this.loggerAdapters = const [],
}) : _options = options;

/// You can retrieve the [PushNotificationMasamuneAdapter] first given by [MasamuneAdapterScope].
Expand Down Expand Up @@ -168,6 +169,9 @@ class PushNotificationMasamuneAdapter extends MasamuneAdapter {
/// **Android**でのみサポートされる通知チャンネルの説明です。
final String androidNotificationChannelDescription;

@override
final List<LoggerAdapter> loggerAdapters;

/// Specify the object of [PushNotification].
///
/// After specifying this, execute [onMaybeBoot] to start initialization automatically.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ part 'functions/send_notification_functions_action.dart';

part 'src/push_notification.dart';
part 'src/push_notification_value.dart';
part 'src/push_notification_logger_event.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
///
/// この端末のFCMトークンを取得します。
Future<String?> getToken() {
return _messaging.getToken();
final token = _messaging.getToken();
_sendLog(PushNotificationLoggerEvent.token, parameters: {
PushNotificationLoggerEvent.tokenKey: token,
});
return token;
}

/// Start receiving notifications.
Expand Down Expand Up @@ -162,6 +166,7 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
sound: true,
);
_listening = true;
_sendLog(PushNotificationLoggerEvent.listen, parameters: {});
_completer?.complete();
_completer = null;
} catch (e) {
Expand Down Expand Up @@ -204,6 +209,11 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
data: data,
),
);
_sendLog(PushNotificationLoggerEvent.send, parameters: {
PushNotificationLoggerEvent.titleKey: title,
PushNotificationLoggerEvent.bodyKey: text,
PushNotificationLoggerEvent.toKey: target,
});
}

/// Subscribe to a topic named [topic].
Expand All @@ -221,6 +231,9 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
await listen();
assert(topic.isNotEmpty, "You have not specified a topic.");
_messaging.subscribeToTopic(topic);
_sendLog(PushNotificationLoggerEvent.subscribe, parameters: {
PushNotificationLoggerEvent.topicNameKey: topic,
});
}

/// Unsubscribe from a topic name named [topic].
Expand All @@ -234,6 +247,9 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
await listen();
assert(topic.isNotEmpty, "You have not specified a topic.");
_messaging.unsubscribeFromTopic(topic);
_sendLog(PushNotificationLoggerEvent.unsubscribe, parameters: {
PushNotificationLoggerEvent.topicNameKey: topic,
});
}

Future<void> _onMessage(RemoteMessage message) async {
Expand All @@ -245,6 +261,11 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
target: message.from ?? "",
whenAppOpened: false,
);
_sendLog(PushNotificationLoggerEvent.receive, parameters: {
PushNotificationLoggerEvent.titleKey: message.notification?.title ?? "",
PushNotificationLoggerEvent.bodyKey: message.notification?.body ?? "",
PushNotificationLoggerEvent.toKey: message.from ?? "",
});
notifyListeners();
}

Expand All @@ -257,8 +278,20 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
target: message.from ?? "",
whenAppOpened: true,
);
_sendLog(PushNotificationLoggerEvent.receive, parameters: {
PushNotificationLoggerEvent.titleKey: message.notification?.title ?? "",
PushNotificationLoggerEvent.bodyKey: message.notification?.body ?? "",
PushNotificationLoggerEvent.toKey: message.from ?? "",
});
notifyListeners();
}

void _sendLog(PushNotificationLoggerEvent event, {DynamicMap? parameters}) {
final loggerAdapters = LoggerAdapter.primary;
for (final loggerAdapter in loggerAdapters) {
loggerAdapter.send(event.toString(), parameters: parameters);
}
}
}

@immutable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
part of masamune_notification_firebase;

/// Push notification event for logging.
///
/// ロギング用のPUSH通知イベント。
enum PushNotificationLoggerEvent {
/// When a topic is subscribed to.
///
/// トピックが購読されたとき。
subscribe,

/// When a topic is unsubscribed.
///
/// トピックが購読解除されたとき。
unsubscribe,

/// When a notification is sent.
///
/// 通知が送信されたとき。
send,

/// When a notification is received.
///
/// 監視を開始したとき。
listen,

/// When a notification is received.
///
/// 通知を受信したとき。
receive,

/// When a token is received.
///
/// トークンを受信したとき。
token;

/// Topic name key.
///
/// トピック名のキー。
static const topicNameKey = "topic";

/// Key to the title of the notification.
///
/// 通知のタイトルのキー。
static const titleKey = "title";

/// Key to the text of the notice.
///
/// 通知の文章のキー。
static const bodyKey = "body";

/// Notification Destination Key
///
/// 通知の宛先のキー
static const toKey = "to";

/// Key of the token.
///
/// トークンのキー。
static const tokenKey = "token";
}
4 changes: 2 additions & 2 deletions packages/masamune_notification_firebase/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ packages:
path: "../katana_router"
relative: true
source: path
version: "2.0.32"
version: "2.0.33"
katana_router_annotation:
dependency: "direct overridden"
description:
Expand Down Expand Up @@ -543,7 +543,7 @@ packages:
path: "../masamune"
relative: true
source: path
version: "2.4.39"
version: "2.4.40"
masamune_annotation:
dependency: "direct overridden"
description:
Expand Down

0 comments on commit 6b7e70e

Please sign in to comment.