Skip to content

Commit

Permalink
notif: Replace flutter_local_notification createNotificationChannel
Browse files Browse the repository at this point in the history
… with pigeon

Updates zulip#351
  • Loading branch information
rajveermalviya committed Jul 8, 2024
1 parent 73da7e0 commit 2ef9e12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
18 changes: 8 additions & 10 deletions lib/notifications/display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ class NotificationChannelManager {
// channel ID and delete it. See zulip-mobile's `createNotificationChannel`
// in android/app/src/main/java/com/zulipmobile/notifications/NotificationChannelManager.kt .
static Future<void> _ensureChannel() async {
final plugin = ZulipBinding.instance.notifications;
await plugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(AndroidNotificationChannel(
kChannelId,
'Messages', // TODO(i18n)
importance: Importance.high,
enableLights: true,
vibrationPattern: kVibrationPattern,
// TODO(#340) sound
));
await ZulipBinding.instance.androidNotificationHost.createNotificationChannel(NotificationChannel(
id: kChannelId,
name: 'Messages', // TODO(i18n)
importance: NotificationImportance.high,
lightsEnabled: true,
vibrationPattern: kVibrationPattern,
// TODO(#340) sound
));
}
}

Expand Down
31 changes: 13 additions & 18 deletions test/model/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,21 +460,6 @@ class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNo
}

class FakeAndroidFlutterLocalNotificationsPlugin extends Fake implements AndroidFlutterLocalNotificationsPlugin {
/// Consume the log of calls made to [createNotificationChannel].
///
/// This returns a list of the arguments to all calls made
/// to [createNotificationChannel] since the last call to this method.
List<AndroidNotificationChannel> takeCreatedChannels() {
final result = _createdChannels;
_createdChannels = [];
return result;
}
List<AndroidNotificationChannel> _createdChannels = [];

@override
Future<void> createNotificationChannel(AndroidNotificationChannel notificationChannel) async {
_createdChannels.add(notificationChannel);
}
}

class FakeIOSFlutterLocalNotificationsPlugin extends Fake implements IOSFlutterLocalNotificationsPlugin {
Expand All @@ -492,6 +477,17 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
}
List<AndroidNotificationHostApiNotifyCall> _notifyCalls = [];

/// Consume the log of calls made to [createNotificationChannel].
///
/// This returns a list of the arguments to all calls made
/// to [createNotificationChannel] since the last call to this method.
List<NotificationChannel> takeCreatedChannels() {
final result = _createdChannels;
_createdChannels = [];
return result;
}
List<NotificationChannel> _createdChannels = [];

@override
Future<void> notify({
String? tag,
Expand Down Expand Up @@ -526,9 +522,8 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
}

@override
Future<void> createNotificationChannel(NotificationChannel channel) {
// TODO: implement createNotificationChannel
throw UnimplementedError();
Future<void> createNotificationChannel(NotificationChannel channel) async {
_createdChannels.add(channel);
}
}

Expand Down
33 changes: 7 additions & 26 deletions test/notifications/display_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ import '../test_navigation.dart';
import '../widgets/message_list_checks.dart';
import '../widgets/page_checks.dart';

FakeAndroidFlutterLocalNotificationsPlugin get notifAndroid =>
testBinding.notifications
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
as FakeAndroidFlutterLocalNotificationsPlugin;

MessageFcmMessage messageFcmMessage(
Message zulipMessage, {
String? streamName,
Expand Down Expand Up @@ -87,20 +82,13 @@ void main() {
group('NotificationChannelManager', () {
test('smoke', () async {
await init();
check(notifAndroid.takeCreatedChannels()).single
check(testBinding.androidNotificationHost.takeCreatedChannels()).single
..id.equals(NotificationChannelManager.kChannelId)
..name.equals('Messages')
..description.isNull()
..groupId.isNull()
..importance.equals(Importance.high)
..playSound.isTrue()
..sound.isNull()
..enableVibration.isTrue()
..importance.equals(NotificationImportance.high)
..lightsEnabled.equals(true)
..vibrationPattern.isNotNull().deepEquals(
NotificationChannelManager.kVibrationPattern)
..showBadge.isTrue()
..enableLights.isTrue()
..ledColor.isNull()
;
});
});
Expand Down Expand Up @@ -375,19 +363,12 @@ void main() {
});
}

extension AndroidNotificationChannelChecks on Subject<AndroidNotificationChannel> {
extension NotificationChannelChecks on Subject<NotificationChannel> {
Subject<String> get id => has((x) => x.id, 'id');
Subject<String> get name => has((x) => x.name, 'name');
Subject<String?> get description => has((x) => x.description, 'description');
Subject<String?> get groupId => has((x) => x.groupId, 'groupId');
Subject<Importance> get importance => has((x) => x.importance, 'importance');
Subject<bool> get playSound => has((x) => x.playSound, 'playSound');
Subject<AndroidNotificationSound?> get sound => has((x) => x.sound, 'sound');
Subject<bool> get enableVibration => has((x) => x.enableVibration, 'enableVibration');
Subject<bool> get enableLights => has((x) => x.enableLights, 'enableLights');
Subject<int> get importance => has((x) => x.importance, 'importance');
Subject<String?> get name => has((x) => x.name, 'name');
Subject<bool?> get lightsEnabled => has((x) => x.lightsEnabled, 'lightsEnabled');
Subject<Int64List?> get vibrationPattern => has((x) => x.vibrationPattern, 'vibrationPattern');
Subject<Color?> get ledColor => has((x) => x.ledColor, 'ledColor');
Subject<bool> get showBadge => has((x) => x.showBadge, 'showBadge');
}

extension on Subject<AndroidNotificationHostApiNotifyCall> {
Expand Down

0 comments on commit 2ef9e12

Please sign in to comment.