Skip to content

Commit

Permalink
fix: Added callbacks for message retrieval.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Aug 25, 2023
1 parent 4de29a9 commit 0c914dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PushNotificationMasamuneAdapter extends MasamuneAdapter {
this.macosOptions,
this.linuxOptions,
this.loggerAdapters = const [],
this.onLink,
}) : _options = options;

/// You can retrieve the [PushNotificationMasamuneAdapter] first given by [MasamuneAdapterScope].
Expand Down Expand Up @@ -186,6 +187,11 @@ class PushNotificationMasamuneAdapter extends MasamuneAdapter {
/// [pushNotification]が設定されている場合、[onMaybeBoot]を実行した際合わせてトピックの購読を行う際のトピックリストを指定します。
final List<String> subscribeOnBoot;

/// Callback when the URL is launched.
///
/// URLが起動されたときのコールバック。
final FutureOr<void> Function(Uri link)? onLink;

@override
void onInitScope(MasamuneAdapter adapter) {
super.onInitScope(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
return FirebaseMessaging.instance;
}

static const String _linkKey = "@link";

@override
void dispose() {
super.dispose();
Expand Down Expand Up @@ -131,7 +133,6 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
}
_completer = Completer();
try {
final adapter = primaryAdapter;
await FirebaseCore.initialize(options: adapter.options);
await _messaging.setAutoInitEnabled(true);
_onMessageSubscription = FirebaseMessaging.onMessage.listen(_onMessage);
Expand Down Expand Up @@ -197,6 +198,7 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
String? channel,
DynamicMap? data,
required String target,
Uri? link,
}) async {
await listen();
final f = adapter.functions ?? Functions();
Expand All @@ -206,7 +208,10 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
text: text,
target: target,
channel: adapter.androidNotificationChannelId,
data: data,
data: {
if (data != null) ...data,
if (link != null) _linkKey: link.path,
},
),
);
_sendLog(PushNotificationLoggerEvent.send, parameters: {
Expand Down Expand Up @@ -261,6 +266,12 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
target: message.from ?? "",
whenAppOpened: false,
);
if (adapter.onLink != null && data.containsKey(_linkKey)) {
final uri = data.get(_linkKey, "").toUri();
if (uri != null) {
await adapter.onLink?.call(uri);
}
}
_sendLog(PushNotificationLoggerEvent.receive, parameters: {
PushNotificationLoggerEvent.titleKey: message.notification?.title ?? "",
PushNotificationLoggerEvent.bodyKey: message.notification?.body ?? "",
Expand All @@ -278,6 +289,12 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
target: message.from ?? "",
whenAppOpened: true,
);
if (adapter.onLink != null && data.containsKey(_linkKey)) {
final uri = data.get(_linkKey, "").toUri();
if (uri != null) {
await adapter.onLink?.call(uri);
}
}
_sendLog(PushNotificationLoggerEvent.receive, parameters: {
PushNotificationLoggerEvent.titleKey: message.notification?.title ?? "",
PushNotificationLoggerEvent.bodyKey: message.notification?.body ?? "",
Expand Down
8 changes: 4 additions & 4 deletions packages/masamune_notification_firebase/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ packages:
path: "../katana_form"
relative: true
source: path
version: "2.7.5"
version: "2.7.6"
katana_functions:
dependency: "direct overridden"
description:
Expand Down Expand Up @@ -471,14 +471,14 @@ packages:
path: "../katana_router"
relative: true
source: path
version: "2.0.33"
version: "2.0.34"
katana_router_annotation:
dependency: "direct overridden"
description:
path: "../katana_router_annotation"
relative: true
source: path
version: "2.0.18"
version: "2.0.19"
katana_scoped:
dependency: "direct overridden"
description:
Expand Down Expand Up @@ -543,7 +543,7 @@ packages:
path: "../masamune"
relative: true
source: path
version: "2.4.40"
version: "2.4.41"
masamune_annotation:
dependency: "direct overridden"
description:
Expand Down

0 comments on commit 0c914dd

Please sign in to comment.