Skip to content

Commit 59d248d

Browse files
notif: Replace flutter_local_notification createNotificationChannel with pigeon
Updates #351
1 parent 4043060 commit 59d248d

File tree

3 files changed

+20
-81
lines changed

3 files changed

+20
-81
lines changed

lib/notifications/display.dart

+8-10
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,14 @@ class NotificationChannelManager {
5353
// channel ID and delete it. See zulip-mobile's `createNotificationChannel`
5454
// in android/app/src/main/java/com/zulipmobile/notifications/NotificationChannelManager.kt .
5555
static Future<void> _ensureChannel() async {
56-
final plugin = ZulipBinding.instance.notifications;
57-
await plugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
58-
?.createNotificationChannel(AndroidNotificationChannel(
59-
kChannelId,
60-
'Messages', // TODO(i18n)
61-
importance: Importance.high,
62-
enableLights: true,
63-
vibrationPattern: kVibrationPattern,
64-
// TODO(#340) sound
65-
));
56+
await ZulipBinding.instance.androidNotificationHost.createNotificationChannel(NotificationChannel(
57+
id: kChannelId,
58+
name: 'Messages', // TODO(i18n)
59+
importance: NotificationImportance.high,
60+
lightsEnabled: true,
61+
vibrationPattern: kVibrationPattern,
62+
// TODO(#340) sound
63+
));
6664
}
6765
}
6866

test/model/binding.dart

+5-45
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'package:firebase_core/firebase_core.dart';
44
import 'package:firebase_messaging/firebase_messaging.dart';
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter_local_notifications/flutter_local_notifications.dart' hide Person;
7-
import 'package:flutter_local_notifications_platform_interface/flutter_local_notifications_platform_interface.dart';
87
import 'package:test/fake.dart';
98
import 'package:url_launcher/url_launcher.dart' as url_launcher;
109
import 'package:zulip/host/android_notifications.dart';
@@ -513,34 +512,6 @@ class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNo
513512
return true;
514513
}
515514

516-
FlutterLocalNotificationsPlatform? _platform;
517-
518-
@override
519-
T? resolvePlatformSpecificImplementation<T extends FlutterLocalNotificationsPlatform>() {
520-
// This follows the logic of the base class's implementation,
521-
// but supplies our fakes for the per-platform classes.
522-
assert(initializationSettings != null);
523-
assert(T != FlutterLocalNotificationsPlatform);
524-
if (kIsWeb) return null;
525-
switch (defaultTargetPlatform) {
526-
case TargetPlatform.android:
527-
assert(_platform == null || _platform is FakeAndroidFlutterLocalNotificationsPlugin);
528-
if (T != AndroidFlutterLocalNotificationsPlugin) return null;
529-
return (_platform ??= FakeAndroidFlutterLocalNotificationsPlugin()) as T?;
530-
531-
case TargetPlatform.iOS:
532-
assert(_platform == null || _platform is FakeIOSFlutterLocalNotificationsPlugin);
533-
if (T != IOSFlutterLocalNotificationsPlugin) return null;
534-
return (_platform ??= FakeIOSFlutterLocalNotificationsPlugin()) as T?;
535-
536-
case TargetPlatform.linux:
537-
case TargetPlatform.macOS:
538-
case TargetPlatform.windows:
539-
case TargetPlatform.fuchsia:
540-
return null;
541-
}
542-
}
543-
544515
/// The value to be returned by [getNotificationAppLaunchDetails].
545516
NotificationAppLaunchDetails? appLaunchDetails;
546517

@@ -556,32 +527,21 @@ class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNo
556527
}
557528
}
558529

559-
class FakeAndroidFlutterLocalNotificationsPlugin extends Fake implements AndroidFlutterLocalNotificationsPlugin {
530+
class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
560531
/// Consume the log of calls made to [createNotificationChannel].
561532
///
562533
/// This returns a list of the arguments to all calls made
563534
/// to [createNotificationChannel] since the last call to this method.
564-
List<AndroidNotificationChannel> takeCreatedChannels() {
535+
List<NotificationChannel> takeCreatedChannels() {
565536
final result = _createdChannels;
566537
_createdChannels = [];
567538
return result;
568539
}
569-
List<AndroidNotificationChannel> _createdChannels = [];
540+
List<NotificationChannel> _createdChannels = [];
570541

571542
@override
572-
Future<void> createNotificationChannel(AndroidNotificationChannel notificationChannel) async {
573-
_createdChannels.add(notificationChannel);
574-
}
575-
}
576-
577-
class FakeIOSFlutterLocalNotificationsPlugin extends Fake implements IOSFlutterLocalNotificationsPlugin {
578-
}
579-
580-
class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
581-
@override
582-
Future<void> createNotificationChannel(NotificationChannel channel) {
583-
// TODO: implement createNotificationChannel
584-
throw UnimplementedError();
543+
Future<void> createNotificationChannel(NotificationChannel channel) async {
544+
_createdChannels.add(channel);
585545
}
586546

587547
/// Consume the log of calls made to [notify].

test/notifications/display_test.dart

+7-26
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ import '../test_navigation.dart';
2929
import '../widgets/message_list_checks.dart';
3030
import '../widgets/page_checks.dart';
3131

32-
FakeAndroidFlutterLocalNotificationsPlugin get notifAndroid =>
33-
testBinding.notifications
34-
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
35-
as FakeAndroidFlutterLocalNotificationsPlugin;
36-
3732
MessageFcmMessage messageFcmMessage(
3833
Message zulipMessage, {
3934
String? streamName,
@@ -90,20 +85,13 @@ void main() {
9085
group('NotificationChannelManager', () {
9186
test('smoke', () async {
9287
await init();
93-
check(notifAndroid.takeCreatedChannels()).single
88+
check(testBinding.androidNotificationHost.takeCreatedChannels()).single
9489
..id.equals(NotificationChannelManager.kChannelId)
9590
..name.equals('Messages')
96-
..description.isNull()
97-
..groupId.isNull()
98-
..importance.equals(Importance.high)
99-
..playSound.isTrue()
100-
..sound.isNull()
101-
..enableVibration.isTrue()
91+
..importance.equals(NotificationImportance.high)
92+
..lightsEnabled.equals(true)
10293
..vibrationPattern.isNotNull().deepEquals(
10394
NotificationChannelManager.kVibrationPattern)
104-
..showBadge.isTrue()
105-
..enableLights.isTrue()
106-
..ledColor.isNull()
10795
;
10896
});
10997
});
@@ -462,19 +450,12 @@ void main() {
462450
});
463451
}
464452

465-
extension AndroidNotificationChannelChecks on Subject<AndroidNotificationChannel> {
453+
extension NotificationChannelChecks on Subject<NotificationChannel> {
466454
Subject<String> get id => has((x) => x.id, 'id');
467-
Subject<String> get name => has((x) => x.name, 'name');
468-
Subject<String?> get description => has((x) => x.description, 'description');
469-
Subject<String?> get groupId => has((x) => x.groupId, 'groupId');
470-
Subject<Importance> get importance => has((x) => x.importance, 'importance');
471-
Subject<bool> get playSound => has((x) => x.playSound, 'playSound');
472-
Subject<AndroidNotificationSound?> get sound => has((x) => x.sound, 'sound');
473-
Subject<bool> get enableVibration => has((x) => x.enableVibration, 'enableVibration');
474-
Subject<bool> get enableLights => has((x) => x.enableLights, 'enableLights');
455+
Subject<int> get importance => has((x) => x.importance, 'importance');
456+
Subject<String?> get name => has((x) => x.name, 'name');
457+
Subject<bool?> get lightsEnabled => has((x) => x.lightsEnabled, 'lightsEnabled');
475458
Subject<Int64List?> get vibrationPattern => has((x) => x.vibrationPattern, 'vibrationPattern');
476-
Subject<Color?> get ledColor => has((x) => x.ledColor, 'ledColor');
477-
Subject<bool> get showBadge => has((x) => x.showBadge, 'showBadge');
478459
}
479460

480461
extension on Subject<AndroidNotificationHostApiNotifyCall> {

0 commit comments

Comments
 (0)