Skip to content

Commit

Permalink
fix: Setup badges and sounds for PUSH notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Mar 14, 2024
1 parent 77fdcc7 commit acd31c1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SendNotificationFunctionsAction
this.tokens,
this.topic,
this.channel,
this.badgeCount,
this.sound = PushNotificationSound.defaultSound,
this.data,
}) : assert(
tokens != null || topic != null,
Expand Down Expand Up @@ -58,6 +60,16 @@ class SendNotificationFunctionsAction
/// PUSH通知の送信先(トークン)
final ModelToken? tokens;

/// Number of badges to display in PUSH notifications.
///
/// PUSH通知で表示するバッジの数。
final int? badgeCount;

/// Sound of PUSH notifications.
///
/// PUSH通知のサウンド。
final PushNotificationSound sound;

@override
String get action => "send_notification";

Expand All @@ -69,6 +81,8 @@ class SendNotificationFunctionsAction
if (channel != null) "channel_id": channel,
if (data != null) "data": data,
if (tokens != null) "token": tokens!.value else "topic": topic,
if(badgeCount != null) "badge": badgeCount,
if (sound != PushNotificationSound.none) "sound": sound.value,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ part 'src/push_notification.dart';
part 'src/push_notification_masamune_adapter.dart';
part 'src/push_notification_value.dart';
part 'src/push_notification_logger_event.dart';
part 'src/push_notification_sound.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Package imports:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:masamune/masamune.dart';
import 'package:masamune_notification/masamune_notification.dart';
import 'package:masamune_scheduler/masamune_scheduler.dart';

part 'push_notification_schedule.m.dart';
Expand Down Expand Up @@ -181,6 +182,8 @@ class ModelServerCommandPushNotificationSchedule
String? topic,
ModelToken? tokens,
Uri? link,
int? badgeCount,
String? sound,
}) = _ModelServerCommandPushNotificationSchedule;

const ModelServerCommandPushNotificationSchedule._()
Expand Down Expand Up @@ -238,6 +241,16 @@ class ModelServerCommandPushNotificationSchedule
/// 通知から遷移するリンク。
Uri? get link => throw UnimplementedError();

/// Number of badges to display in PUSH notifications.
///
/// PUSH通知で表示するバッジの数。
int? get badgeCount => throw UnimplementedError();

/// Sound of PUSH notifications.
///
/// PUSH通知のサウンド。
String? get sound => throw UnimplementedError();

/// Command Name.
///
/// コマンド名。
Expand All @@ -248,6 +261,8 @@ class ModelServerCommandPushNotificationSchedule
static const String _kTitleKey = "title";
static const String _kTextKey = "text";
static const String _kChannelIdKey = "channelId";
static const String _kSoundKey = "sound";
static const String _kBadgeCountKey = "badgeCount";
static const String _kDataKey = "data";
static const String _kTopicKey = "topic";
static const String _kTokenKey = "token";
Expand Down Expand Up @@ -277,6 +292,8 @@ class ModelServerCommandPushNotificationSchedule
},
_kTokenKey: tokens?.value,
_kTopicKey: topic,
if (sound != null) _kSoundKey: sound,
if (badgeCount != null) _kBadgeCountKey: badgeCount,
};
}

Expand All @@ -300,6 +317,8 @@ class _ModelServerCommandPushNotificationSchedule
this.topic,
this.tokens,
this.link,
this.badgeCount,
this.sound,
}) : super._();

@override
Expand All @@ -325,6 +344,12 @@ class _ModelServerCommandPushNotificationSchedule

@override
final ModelToken? tokens;

@override
final int? badgeCount;

@override
final String? sound;
}

/// Abstract class for defining a document with a schedule for PUSH notifications.
Expand Down
10 changes: 10 additions & 0 deletions packages/masamune_notification/lib/src/push_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
///
/// [tokens][topic]は同時に指定することはできません。
///
/// [sound]には通知のサウンドを指定します。[badgeCount]にはバッジに表示する数を指定します。
///
/// 詳しくは[SendNotificationFunctionsAction]を御覧ください。
Future<SendNotificationFunctionsActionResponse> send({
required String title,
Expand All @@ -216,6 +218,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
DynamicMap? data,
String? topic,
ModelToken? tokens,
int? badgeCount,
PushNotificationSound sound = PushNotificationSound.defaultSound,
Uri? link,
}) async {
assert(
Expand Down Expand Up @@ -273,6 +277,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
DynamicMap? data,
String? topic,
ModelToken? tokens,
int? badgeCount,
PushNotificationSound sound = PushNotificationSound.defaultSound,
Uri? link,
}) async {
assert(
Expand Down Expand Up @@ -307,6 +313,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
tokens: tokens,
topic: topic,
link: link,
sound: sound.value,
badgeCount: badgeCount,
),
) ??
PushNotificationScheduleModel(
Expand All @@ -319,6 +327,8 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
tokens: tokens,
topic: topic,
link: link,
sound: sound.value,
badgeCount: badgeCount,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
part of '/masamune_notification.dart';

/// Sound of push notification.
///
/// PUSH通知のサウンド。
enum PushNotificationSound {
/// No sound.
///
/// サウンドなし。
none,

/// Default sound.
///
/// デフォルトのサウンド。
defaultSound;

/// Actual value.
///
/// 実際の値。
String? get value {
switch (this) {
case PushNotificationSound.none:
return null;
case PushNotificationSound.defaultSound:
return "default";
}
}
}

0 comments on commit acd31c1

Please sign in to comment.