Merge pull request #691 from zjs81/fix/notifications-settings-ui
Check OS permission and reveal child toggles in notification settings
This commit is contained in:
6 files changed
+1024
-184
No files matched your search
@@ -102,10 +102,11 @@ Access via **App Settings → Notifications**:
|
||||
|
||||
| Setting | Default | Description |
|
||||
|---|---|---|
|
||||
| Enable Notifications | On | Master toggle; requests OS permission when turned on |
|
||||
| Message Notifications | On | DM alerts (greyed out if master is off) |
|
||||
| Channel Message Notifications | On | Channel alerts (greyed out if master is off) |
|
||||
| Advertisement Notifications | On | New node alerts (greyed out if master is off) |
|
||||
| Enable Notifications | On | Master toggle (shows off if OS permission is denied); requests OS permission when turned on and reveals the options below |
|
||||
| Message Notifications | On | Direct message alerts (shown when notifications are enabled) |
|
||||
| Channel Message Notifications | On | Channel message alerts (shown when notifications are enabled) |
|
||||
| Advertisement Notifications | On | New node alerts (shown when notifications are enabled) |
|
||||
| Background Activity (Android) | — | Opens system battery optimization settings (shown when notifications are enabled) |
|
||||
|
||||
### Per-Channel Notification Modes
|
||||
|
||||
|
||||
@@ -63,9 +63,9 @@ class AppSettingsScreen extends StatelessWidget {
|
||||
|
||||
// NOTIFICATIONS
|
||||
SectionHeader(context.l10n.appSettings_notifications),
|
||||
MeshCard(
|
||||
const MeshCard(
|
||||
padding: EdgeInsets.zero,
|
||||
child: _settingsCard(_buildNotificationsContent),
|
||||
child: NotificationsSettingsSection(),
|
||||
),
|
||||
|
||||
// MESSAGING
|
||||
@@ -266,156 +266,6 @@ class AppSettingsScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNotificationsContent(
|
||||
BuildContext context,
|
||||
AppSettingsService settingsService,
|
||||
) {
|
||||
final notifEnabled = settingsService.settings.notificationsEnabled;
|
||||
return Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: const Icon(Icons.notifications_outlined, size: 24),
|
||||
title: Text(context.l10n.appSettings_enableNotifications),
|
||||
subtitle: Text(context.l10n.appSettings_enableNotificationsSubtitle),
|
||||
value: settingsService.settings.notificationsEnabled,
|
||||
onChanged: (value) async {
|
||||
if (value) {
|
||||
final granted = await NotificationService().requestPermissions();
|
||||
if (!granted) {
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
context.l10n.appSettings_notificationPermissionDenied,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
await settingsService.setNotificationsEnabled(value);
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
value
|
||||
? context.l10n.appSettings_notificationsEnabled
|
||||
: context.l10n.appSettings_notificationsDisabled,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.message_outlined,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_messageNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_messageNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewMessage,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewMessage(value)
|
||||
: null,
|
||||
),
|
||||
if (PlatformInfo.isAndroid) ...[
|
||||
const Divider(height: 1, indent: 16),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
leading: const Icon(Icons.battery_saver_outlined, size: 24),
|
||||
title: Text(context.l10n.appSettings_batteryOptimization),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_batteryOptimizationSubtitle,
|
||||
),
|
||||
trailing: const Icon(Icons.open_in_new, size: 24),
|
||||
onTap: BackgroundService.openBatteryOptimizationSettings,
|
||||
),
|
||||
],
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.forum_outlined,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_channelMessageNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_channelMessageNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewChannelMessage,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewChannelMessage(value)
|
||||
: null,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.cell_tower,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_advertisementNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_advertisementNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewAdvert,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewAdvert(value)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMessagingContent(
|
||||
BuildContext context,
|
||||
AppSettingsService settingsService,
|
||||
@@ -1870,3 +1720,205 @@ class _ScrollIntoViewOnceState extends State<_ScrollIntoViewOnce> {
|
||||
@override
|
||||
Widget build(BuildContext context) => widget.child;
|
||||
}
|
||||
|
||||
class NotificationsSettingsSection extends StatefulWidget {
|
||||
final Future<bool> Function()? hasPermission;
|
||||
final Future<bool> Function()? requestPermission;
|
||||
|
||||
const NotificationsSettingsSection({
|
||||
super.key,
|
||||
this.hasPermission,
|
||||
this.requestPermission,
|
||||
});
|
||||
|
||||
@override
|
||||
State<NotificationsSettingsSection> createState() =>
|
||||
_NotificationsSettingsSectionState();
|
||||
}
|
||||
|
||||
class _NotificationsSettingsSectionState
|
||||
extends State<NotificationsSettingsSection>
|
||||
with WidgetsBindingObserver {
|
||||
bool _hasNotificationPermission = false;
|
||||
bool _permissionCheckComplete = false;
|
||||
int _permissionCheckGeneration = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_refreshNotificationPermission(inInit: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant NotificationsSettingsSection oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.hasPermission != oldWidget.hasPermission) {
|
||||
_refreshNotificationPermission();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
_refreshNotificationPermission();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _checkPermission() {
|
||||
final checker = widget.hasPermission;
|
||||
if (checker != null) return checker();
|
||||
return NotificationService().hasNotificationPermission();
|
||||
}
|
||||
|
||||
Future<bool> _requestPermission() {
|
||||
final requester = widget.requestPermission;
|
||||
if (requester != null) return requester();
|
||||
return NotificationService().requestPermissions();
|
||||
}
|
||||
|
||||
void _refreshNotificationPermission({bool inInit = false}) {
|
||||
final generation = ++_permissionCheckGeneration;
|
||||
var completedSynchronously = false;
|
||||
var syncResult = false;
|
||||
var isSyncWindow = true;
|
||||
_checkPermission().then((granted) {
|
||||
if (generation != _permissionCheckGeneration) return;
|
||||
if (isSyncWindow && inInit) {
|
||||
completedSynchronously = true;
|
||||
syncResult = granted;
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (_permissionCheckComplete && _hasNotificationPermission == granted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_hasNotificationPermission = granted;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
});
|
||||
isSyncWindow = false;
|
||||
if (completedSynchronously) {
|
||||
_hasNotificationPermission = syncResult;
|
||||
_permissionCheckComplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final settingsService = context.watch<AppSettingsService>();
|
||||
final settings = settingsService.settings;
|
||||
final l10n = context.l10n;
|
||||
final effectiveEnabled =
|
||||
settings.notificationsEnabled && _hasNotificationPermission;
|
||||
final permissionDenied =
|
||||
_permissionCheckComplete && !_hasNotificationPermission;
|
||||
const tilePadding = EdgeInsets.symmetric(horizontal: 16, vertical: 4);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.notifications_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_enableNotifications),
|
||||
subtitle: Text(
|
||||
permissionDenied
|
||||
? l10n.appSettings_notificationPermissionDenied
|
||||
: l10n.appSettings_enableNotificationsSubtitle,
|
||||
),
|
||||
value: effectiveEnabled,
|
||||
onChanged: (value) async {
|
||||
_permissionCheckGeneration++;
|
||||
if (value) {
|
||||
final granted = await _requestPermission();
|
||||
if (!granted) {
|
||||
if (!context.mounted) return;
|
||||
setState(() {
|
||||
_hasNotificationPermission = false;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.appSettings_notificationPermissionDenied),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
_hasNotificationPermission = true;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
await settingsService.setNotificationsEnabled(value);
|
||||
if (!context.mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
value
|
||||
? l10n.appSettings_notificationsEnabled
|
||||
: l10n.appSettings_notificationsDisabled,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
},
|
||||
),
|
||||
SettingsReveal(
|
||||
visible: effectiveEnabled,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.message_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_messageNotifications),
|
||||
subtitle: Text(l10n.appSettings_messageNotificationsSubtitle),
|
||||
value: settings.notifyOnNewMessage,
|
||||
onChanged: settingsService.setNotifyOnNewMessage,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.forum_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_channelMessageNotifications),
|
||||
subtitle: Text(
|
||||
l10n.appSettings_channelMessageNotificationsSubtitle,
|
||||
),
|
||||
value: settings.notifyOnNewChannelMessage,
|
||||
onChanged: settingsService.setNotifyOnNewChannelMessage,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.cell_tower, size: 24),
|
||||
title: Text(l10n.appSettings_advertisementNotifications),
|
||||
subtitle: Text(
|
||||
l10n.appSettings_advertisementNotificationsSubtitle,
|
||||
),
|
||||
value: settings.notifyOnNewAdvert,
|
||||
onChanged: settingsService.setNotifyOnNewAdvert,
|
||||
),
|
||||
if (PlatformInfo.isAndroid) ...[
|
||||
const Divider(height: 1, indent: 16),
|
||||
ListTile(
|
||||
contentPadding: tilePadding,
|
||||
leading: const Icon(Icons.battery_saver_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_batteryOptimization),
|
||||
subtitle: Text(l10n.appSettings_batteryOptimizationSubtitle),
|
||||
trailing: const Icon(Icons.open_in_new, size: 24),
|
||||
onTap: BackgroundService.openBatteryOptimizationSettings,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -180,7 +180,9 @@ class NotificationService {
|
||||
// connection whose async subscription can throw an unhandled
|
||||
// SocketException when the bus socket is missing (e.g. running as
|
||||
// root or inside a container without a session bus).
|
||||
if (PlatformInfo.isLinux && !_isDbusSessionAvailable()) {
|
||||
if (PlatformInfo.isLinux &&
|
||||
defaultTargetPlatform == TargetPlatform.linux &&
|
||||
!_isDbusSessionAvailable()) {
|
||||
debugPrint('Skipping notification init: D-Bus session bus unavailable');
|
||||
return;
|
||||
}
|
||||
@@ -250,11 +252,97 @@ class NotificationService {
|
||||
// Cached "are we allowed to post notifications" result. Null = not yet
|
||||
// determined. Avoids calling _notifications.show() when it would only throw
|
||||
// "You must request notifications permissions first" (every web build).
|
||||
// Android denials are never cached so a later grant in system settings is
|
||||
// picked up without restarting the app.
|
||||
// Denials are never cached so a later grant in system settings is picked up
|
||||
// without restarting the app, and [hasNotificationPermission] refreshes or
|
||||
// clears [_canNotify] whenever live OS permission is checked.
|
||||
bool? _canNotify;
|
||||
static const _permissionRequestedKey = 'notification_permission_requested';
|
||||
|
||||
@visibleForTesting
|
||||
Future<bool> Function()? permissionCheckerForTest;
|
||||
|
||||
@visibleForTesting
|
||||
Future<bool> Function()? permissionRequesterForTest;
|
||||
|
||||
@visibleForTesting
|
||||
Future<bool> ensureCanNotifyForTest() => _ensureCanNotify();
|
||||
|
||||
@visibleForTesting
|
||||
bool? get canNotifyCacheForTest => _canNotify;
|
||||
|
||||
@visibleForTesting
|
||||
void resetForTest() {
|
||||
_canNotify = null;
|
||||
_isInitialized = false;
|
||||
permissionCheckerForTest = null;
|
||||
permissionRequesterForTest = null;
|
||||
}
|
||||
|
||||
T? _resolvePlugin<T extends FlutterLocalNotificationsPlatform>() {
|
||||
try {
|
||||
return _notifications.resolvePlatformSpecificImplementation<T>();
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static bool _isDarwinPermissionGranted(NotificationsEnabledOptions? options) {
|
||||
if (options == null) return false;
|
||||
return options.isEnabled ||
|
||||
options.isProvisionalEnabled ||
|
||||
options.isAlertEnabled ||
|
||||
options.isSoundEnabled ||
|
||||
options.isBadgeEnabled;
|
||||
}
|
||||
|
||||
/// Queries the live OS notification permission state without returning a
|
||||
/// stale cached grant. Updates [_canNotify] so [_ensureCanNotify] reflects
|
||||
/// permissions revoked in system settings.
|
||||
Future<bool> hasNotificationPermission() {
|
||||
final testChecker = permissionCheckerForTest;
|
||||
if (testChecker != null) {
|
||||
return testChecker().then((enabled) {
|
||||
if (!kIsWeb) {
|
||||
_canNotify = enabled ? true : null;
|
||||
}
|
||||
return enabled;
|
||||
});
|
||||
}
|
||||
|
||||
if (kIsWeb) return SynchronousFuture<bool>(true);
|
||||
|
||||
final androidPlugin =
|
||||
_resolvePlugin<AndroidFlutterLocalNotificationsPlugin>();
|
||||
if (androidPlugin != null) {
|
||||
return androidPlugin.areNotificationsEnabled().then((value) {
|
||||
final enabled = value ?? false;
|
||||
_canNotify = enabled ? true : null;
|
||||
return enabled;
|
||||
});
|
||||
}
|
||||
|
||||
final iosPlugin = _resolvePlugin<IOSFlutterLocalNotificationsPlugin>();
|
||||
if (iosPlugin != null) {
|
||||
return iosPlugin.checkPermissions().then((options) {
|
||||
final enabled = _isDarwinPermissionGranted(options);
|
||||
_canNotify = enabled ? true : null;
|
||||
return enabled;
|
||||
});
|
||||
}
|
||||
|
||||
final macPlugin = _resolvePlugin<MacOSFlutterLocalNotificationsPlugin>();
|
||||
if (macPlugin != null) {
|
||||
return macPlugin.checkPermissions().then((options) {
|
||||
final enabled = _isDarwinPermissionGranted(options);
|
||||
_canNotify = enabled ? true : null;
|
||||
return enabled;
|
||||
});
|
||||
}
|
||||
|
||||
_canNotify = true;
|
||||
return SynchronousFuture<bool>(true);
|
||||
}
|
||||
|
||||
Future<bool> _ensureCanNotify() async {
|
||||
if (!await _ensureInitialized()) return false;
|
||||
final cached = _canNotify;
|
||||
@@ -264,50 +352,52 @@ class NotificationService {
|
||||
// Skip silently instead of logging an error per incoming message.
|
||||
if (kIsWeb) return _canNotify = false;
|
||||
|
||||
// On Android 13+ notifications require an explicit grant; reflect the real
|
||||
// OS state so we don't spam failed show() calls when denied.
|
||||
final androidPlugin = _notifications
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin
|
||||
>();
|
||||
if (androidPlugin != null) {
|
||||
final enabled = await androidPlugin.areNotificationsEnabled() ?? false;
|
||||
if (enabled) _canNotify = true;
|
||||
return enabled;
|
||||
}
|
||||
|
||||
// iOS/macOS request permission during initialize(); desktop has no gate.
|
||||
return _canNotify = true;
|
||||
return hasNotificationPermission();
|
||||
}
|
||||
|
||||
Future<bool> requestPermissions() async {
|
||||
final testRequester = permissionRequesterForTest;
|
||||
if (testRequester != null) {
|
||||
final granted = await testRequester();
|
||||
if (!kIsWeb) {
|
||||
_canNotify = granted ? true : null;
|
||||
}
|
||||
return granted;
|
||||
}
|
||||
|
||||
if (!_isInitialized) {
|
||||
await initialize();
|
||||
}
|
||||
|
||||
// Request Android 13+ notification permission
|
||||
final androidPlugin = _notifications
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin
|
||||
>();
|
||||
final androidPlugin =
|
||||
_resolvePlugin<AndroidFlutterLocalNotificationsPlugin>();
|
||||
if (androidPlugin != null) {
|
||||
final granted = await androidPlugin.requestNotificationsPermission();
|
||||
if (granted == true) _canNotify = true;
|
||||
_canNotify = (granted == true) ? true : null;
|
||||
return granted ?? false;
|
||||
}
|
||||
|
||||
// iOS permissions are requested during initialization
|
||||
final iosPlugin = _notifications
|
||||
.resolvePlatformSpecificImplementation<
|
||||
IOSFlutterLocalNotificationsPlugin
|
||||
>();
|
||||
final iosPlugin = _resolvePlugin<IOSFlutterLocalNotificationsPlugin>();
|
||||
if (iosPlugin != null) {
|
||||
final granted = await iosPlugin.requestPermissions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
if (granted == true) _canNotify = true;
|
||||
_canNotify = (granted == true) ? true : null;
|
||||
return granted ?? false;
|
||||
}
|
||||
|
||||
final macPlugin = _resolvePlugin<MacOSFlutterLocalNotificationsPlugin>();
|
||||
if (macPlugin != null) {
|
||||
final granted = await macPlugin.requestPermissions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
_canNotify = (granted == true) ? true : null;
|
||||
return granted ?? false;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,12 @@ class PlatformInfo {
|
||||
static bool get isChrome => isWeb && BrowserDetection.isChrome;
|
||||
|
||||
/// Whether the app is running on Android.
|
||||
static bool get isAndroid => !kIsWeb && Platform.isAndroid;
|
||||
static bool get isAndroid =>
|
||||
debugIsAndroidOverride ?? (!kIsWeb && Platform.isAndroid);
|
||||
|
||||
/// Lets widget tests exercise Android-only UI on a non-Android test host.
|
||||
@visibleForTesting
|
||||
static bool? debugIsAndroidOverride;
|
||||
|
||||
/// Whether the app is running on iOS.
|
||||
static bool get isIOS => !kIsWeb && Platform.isIOS;
|
||||
|
||||
@@ -0,0 +1,498 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/screens/app_settings_screen.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_upload_service.dart';
|
||||
import 'package:meshcore_open/services/notification_service.dart';
|
||||
import 'package:meshcore_open/services/translation_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:meshcore_open/utils/platform_info.dart';
|
||||
import 'package:meshcore_open/widgets/mesh_nav_tile.dart';
|
||||
import 'package:meshcore_open/widgets/mesh_ui.dart';
|
||||
import 'package:meshcore_open/widgets/settings_reveal.dart';
|
||||
|
||||
Future<AppSettingsService> _createSettingsService({
|
||||
bool notificationsEnabled = true,
|
||||
}) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
if (!notificationsEnabled) {
|
||||
await settings.setNotificationsEnabled(false);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
Widget _buildSectionTestApp({
|
||||
required AppSettingsService settings,
|
||||
ThemeData? theme,
|
||||
double textScale = 1.0,
|
||||
Future<bool> Function()? hasPermission,
|
||||
Future<bool> Function()? requestPermission,
|
||||
}) {
|
||||
return ChangeNotifierProvider.value(
|
||||
value: settings,
|
||||
child: MaterialApp(
|
||||
theme: theme ?? MeshTheme.light(),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
builder: (context, child) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: child!,
|
||||
),
|
||||
home: Scaffold(
|
||||
body: MeshSettingsTileTheme(
|
||||
child: SingleChildScrollView(
|
||||
child: MeshCard(
|
||||
padding: EdgeInsets.zero,
|
||||
child: NotificationsSettingsSection(
|
||||
hasPermission: hasPermission,
|
||||
requestPermission: requestPermission,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
NotificationService().resetForTest();
|
||||
PlatformInfo.debugIsAndroidOverride = null;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
NotificationService().resetForTest();
|
||||
PlatformInfo.debugIsAndroidOverride = null;
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'notificationsEnabled + OS permission granted shows switch ON, expanded SettingsReveal, and exact row order including Android Background Activity last',
|
||||
(tester) async {
|
||||
PlatformInfo.debugIsAndroidOverride = true;
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
hasPermission: () async => true,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(NotificationsSettingsSection));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
final enableTile = tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(
|
||||
SwitchListTile,
|
||||
l10n.appSettings_enableNotifications,
|
||||
),
|
||||
);
|
||||
expect(enableTile.value, isTrue);
|
||||
expect(
|
||||
find.text(l10n.appSettings_enableNotificationsSubtitle),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
final reveal = tester.widget<SettingsReveal>(find.byType(SettingsReveal));
|
||||
expect(reveal.visible, isTrue);
|
||||
|
||||
final msgFinder = find.text(l10n.appSettings_messageNotifications);
|
||||
final channelFinder = find.text(
|
||||
l10n.appSettings_channelMessageNotifications,
|
||||
);
|
||||
final advertFinder = find.text(
|
||||
l10n.appSettings_advertisementNotifications,
|
||||
);
|
||||
final bgActivityFinder = find.text(l10n.appSettings_batteryOptimization);
|
||||
|
||||
expect(msgFinder, findsOneWidget);
|
||||
expect(channelFinder, findsOneWidget);
|
||||
expect(advertFinder, findsOneWidget);
|
||||
expect(bgActivityFinder, findsOneWidget);
|
||||
|
||||
final msgY = tester.getTopLeft(msgFinder).dy;
|
||||
final channelY = tester.getTopLeft(channelFinder).dy;
|
||||
final advertY = tester.getTopLeft(advertFinder).dy;
|
||||
final bgActivityY = tester.getTopLeft(bgActivityFinder).dy;
|
||||
|
||||
expect(msgY, lessThan(channelY));
|
||||
expect(channelY, lessThan(advertY));
|
||||
expect(advertY, lessThan(bgActivityY));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'notificationsEnabled + OS permission denied shows switch OFF, hides child toggles, shows denied subtitle, and requests permission on tap',
|
||||
(tester) async {
|
||||
PlatformInfo.debugIsAndroidOverride = true;
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
var requestResult = false;
|
||||
var requestCalls = 0;
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
hasPermission: () async => false,
|
||||
requestPermission: () async {
|
||||
requestCalls++;
|
||||
return requestResult;
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(NotificationsSettingsSection));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
SwitchListTile enableTile() => tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(
|
||||
SwitchListTile,
|
||||
l10n.appSettings_enableNotifications,
|
||||
),
|
||||
);
|
||||
|
||||
// Even though settings.notificationsEnabled is true, switch is OFF when OS permission is denied.
|
||||
expect(settings.settings.notificationsEnabled, isTrue);
|
||||
expect(enableTile().value, isFalse);
|
||||
expect(
|
||||
find.text(l10n.appSettings_notificationPermissionDenied),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
expect(
|
||||
find.text(l10n.appSettings_channelMessageNotifications),
|
||||
findsNothing,
|
||||
);
|
||||
expect(
|
||||
find.text(l10n.appSettings_advertisementNotifications),
|
||||
findsNothing,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_batteryOptimization), findsNothing);
|
||||
// No stray Divider when collapsed.
|
||||
expect(find.byType(Divider), findsNothing);
|
||||
|
||||
// Tapping switch while permission request is denied keeps it OFF and shows snackbar.
|
||||
await tester.tap(find.text(l10n.appSettings_enableNotifications));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(requestCalls, 1);
|
||||
expect(enableTile().value, isFalse);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(SnackBar),
|
||||
matching: find.text(l10n.appSettings_notificationPermissionDenied),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Now grant permission on the next tap: switch turns ON, snackbar appears, and children expand.
|
||||
requestResult = true;
|
||||
await tester.tap(find.text(l10n.appSettings_enableNotifications));
|
||||
await tester.pump();
|
||||
await tester.pump(RevealOnEnable.duration);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(requestCalls, 2);
|
||||
expect(enableTile().value, isTrue);
|
||||
expect(settings.settings.notificationsEnabled, isTrue);
|
||||
expect(
|
||||
find.text(l10n.appSettings_enableNotificationsSubtitle),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(SnackBar),
|
||||
matching: find.text(l10n.appSettings_notificationsEnabled),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
|
||||
expect(
|
||||
find.text(l10n.appSettings_channelMessageNotifications),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text(l10n.appSettings_advertisementNotifications),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_batteryOptimization), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('does not flash ON while async OS permission check is in flight', (
|
||||
tester,
|
||||
) async {
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
final completer = Completer<bool>();
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
hasPermission: () => completer.future,
|
||||
),
|
||||
);
|
||||
|
||||
final context = tester.element(find.byType(NotificationsSettingsSection));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
// While async permission check is in flight, switch is OFF and children are hidden,
|
||||
// and the normal subtitle is shown rather than flashing an error prematurely.
|
||||
final initialTile = tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(SwitchListTile, l10n.appSettings_enableNotifications),
|
||||
);
|
||||
expect(initialTile.value, isFalse);
|
||||
expect(
|
||||
find.text(l10n.appSettings_enableNotificationsSubtitle),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
|
||||
completer.complete(false);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final settledTile = tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(SwitchListTile, l10n.appSettings_enableNotifications),
|
||||
);
|
||||
expect(settledTile.value, isFalse);
|
||||
expect(
|
||||
find.text(l10n.appSettings_notificationPermissionDenied),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'toggling OFF -> ON collapses children out of tree and expands them back',
|
||||
(tester) async {
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
hasPermission: () async => true,
|
||||
requestPermission: () async => true,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(NotificationsSettingsSection));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
|
||||
|
||||
// Toggle OFF
|
||||
await tester.tap(find.text(l10n.appSettings_enableNotifications));
|
||||
await tester.pump();
|
||||
await tester.pump(RevealOnEnable.duration);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(settings.settings.notificationsEnabled, isFalse);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
expect(
|
||||
find.text(l10n.appSettings_channelMessageNotifications),
|
||||
findsNothing,
|
||||
);
|
||||
expect(
|
||||
find.text(l10n.appSettings_advertisementNotifications),
|
||||
findsNothing,
|
||||
);
|
||||
expect(find.byType(Divider), findsNothing);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(SnackBar),
|
||||
matching: find.text(l10n.appSettings_notificationsDisabled),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Toggle back ON
|
||||
await tester.tap(find.text(l10n.appSettings_enableNotifications));
|
||||
await tester.pump();
|
||||
await tester.pump(RevealOnEnable.duration);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(settings.settings.notificationsEnabled, isTrue);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
|
||||
expect(
|
||||
find.text(l10n.appSettings_channelMessageNotifications),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text(l10n.appSettings_advertisementNotifications),
|
||||
findsOneWidget,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'AppLifecycleState.resumed re-checks OS permission for revoke and grant',
|
||||
(tester) async {
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
var osPermissionGranted = true;
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
hasPermission: () async => osPermissionGranted,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(NotificationsSettingsSection));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
SwitchListTile enableTile() => tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(
|
||||
SwitchListTile,
|
||||
l10n.appSettings_enableNotifications,
|
||||
),
|
||||
);
|
||||
|
||||
expect(enableTile().value, isTrue);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
|
||||
|
||||
// User revokes permission in OS settings and resumes app.
|
||||
osPermissionGranted = false;
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
await tester.pump(RevealOnEnable.duration);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(enableTile().value, isFalse);
|
||||
expect(
|
||||
find.text(l10n.appSettings_notificationPermissionDenied),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
|
||||
// User grants permission in OS settings and resumes app.
|
||||
osPermissionGranted = true;
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
await tester.pump(RevealOnEnable.duration);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(enableTile().value, isTrue);
|
||||
expect(
|
||||
find.text(l10n.appSettings_enableNotificationsSubtitle),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'AppSettingsScreen integrates NotificationService permission hook',
|
||||
(tester) async {
|
||||
final settings = await _createSettingsService(notificationsEnabled: true);
|
||||
NotificationService().permissionCheckerForTest = () async => false;
|
||||
|
||||
tester.view.physicalSize = const Size(800, 4000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
final connector = MeshCoreConnector();
|
||||
final translation = TranslationService(settings);
|
||||
final uploads = NetworkMapUploadService(
|
||||
connector: connector,
|
||||
settings: settings,
|
||||
);
|
||||
addTearDown(connector.dispose);
|
||||
addTearDown(translation.dispose);
|
||||
addTearDown(uploads.dispose);
|
||||
|
||||
await tester.pumpWidget(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider.value(value: settings),
|
||||
ChangeNotifierProvider.value(value: connector),
|
||||
ChangeNotifierProvider.value(value: translation),
|
||||
ChangeNotifierProvider.value(value: uploads),
|
||||
],
|
||||
child: MaterialApp(
|
||||
theme: MeshTheme.light(),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const AppSettingsScreen(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(AppSettingsScreen));
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
final enableTile = tester.widget<SwitchListTile>(
|
||||
find.widgetWithText(
|
||||
SwitchListTile,
|
||||
l10n.appSettings_enableNotifications,
|
||||
),
|
||||
);
|
||||
expect(enableTile.value, isFalse);
|
||||
expect(
|
||||
find.text(l10n.appSettings_notificationPermissionDenied),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(l10n.appSettings_messageNotifications), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
for (final isDark in [false, true]) {
|
||||
for (final scale in [1.3, 2.0]) {
|
||||
for (final granted in [true, false]) {
|
||||
testWidgets(
|
||||
'NotificationsSettingsSection fits 320dp at ${scale}x in ${isDark ? "dark" : "light"} theme when permission=${granted ? "granted" : "denied"}',
|
||||
(tester) async {
|
||||
PlatformInfo.debugIsAndroidOverride = true;
|
||||
tester.view.physicalSize = const Size(320, 568);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
final settings = await _createSettingsService(
|
||||
notificationsEnabled: true,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildSectionTestApp(
|
||||
settings: settings,
|
||||
theme: isDark ? MeshTheme.dark() : MeshTheme.light(),
|
||||
textScale: scale,
|
||||
hasPermission: () async => granted,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
// Verify 48dp minimum touch target height on all rendered tiles.
|
||||
final tiles = find.byType(ListTile);
|
||||
expect(tiles, findsWidgets);
|
||||
for (final element in tiles.evaluate()) {
|
||||
final size = tester.getSize(find.byWidget(element.widget));
|
||||
expect(size.height, greaterThanOrEqualTo(48));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/services/notification_service.dart';
|
||||
|
||||
class _NoopNotificationsPlatform extends FlutterLocalNotificationsPlatform {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
const channel = MethodChannel('dexterous.com/flutter/local_notifications');
|
||||
final service = NotificationService();
|
||||
|
||||
setUp(() {
|
||||
service.resetForTest();
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, null);
|
||||
FlutterLocalNotificationsPlatform.instance = _NoopNotificationsPlatform();
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
service.resetForTest();
|
||||
});
|
||||
|
||||
group('NotificationService.hasNotificationPermission', () {
|
||||
test('queries Android areNotificationsEnabled and updates cache', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
AndroidFlutterLocalNotificationsPlugin.registerWith();
|
||||
|
||||
bool? androidEnabled = true;
|
||||
var checkCalls = 0;
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
if (call.method == 'initialize') return true;
|
||||
if (call.method == 'areNotificationsEnabled') {
|
||||
checkCalls++;
|
||||
return androidEnabled;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
expect(await service.hasNotificationPermission(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
expect(checkCalls, 1);
|
||||
|
||||
// Subsequent ensureCanNotify uses the cached grant without re-querying.
|
||||
expect(await service.ensureCanNotifyForTest(), isTrue);
|
||||
expect(checkCalls, 1);
|
||||
|
||||
// Revoking OS permission invalidates the cached true on the next live check.
|
||||
androidEnabled = false;
|
||||
expect(await service.hasNotificationPermission(), isFalse);
|
||||
expect(service.canNotifyCacheForTest, isNull);
|
||||
expect(checkCalls, 2);
|
||||
|
||||
// _ensureCanNotify now re-queries and returns false instead of stale true.
|
||||
expect(await service.ensureCanNotifyForTest(), isFalse);
|
||||
expect(checkCalls, 3);
|
||||
|
||||
// Null response from channel is treated as false.
|
||||
androidEnabled = null;
|
||||
expect(await service.hasNotificationPermission(), isFalse);
|
||||
expect(service.canNotifyCacheForTest, isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'queries iOS checkPermissions and invalidates cache when revoked',
|
||||
() async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
IOSFlutterLocalNotificationsPlugin.registerWith();
|
||||
|
||||
Map<String, Object?>? permissionsResponse = {
|
||||
'isEnabled': true,
|
||||
'isAlertEnabled': true,
|
||||
'isBadgeEnabled': true,
|
||||
'isSoundEnabled': true,
|
||||
'isProvisionalEnabled': false,
|
||||
'isCriticalEnabled': false,
|
||||
'isProvidesAppNotificationSettingsEnabled': false,
|
||||
'isCarPlayEnabled': false,
|
||||
};
|
||||
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
if (call.method == 'initialize') return true;
|
||||
if (call.method == 'checkPermissions') return permissionsResponse;
|
||||
return null;
|
||||
});
|
||||
|
||||
expect(await service.hasNotificationPermission(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
expect(await service.ensureCanNotifyForTest(), isTrue);
|
||||
|
||||
// Provisional permission also counts as granted.
|
||||
permissionsResponse = {
|
||||
'isEnabled': false,
|
||||
'isAlertEnabled': false,
|
||||
'isBadgeEnabled': false,
|
||||
'isSoundEnabled': false,
|
||||
'isProvisionalEnabled': true,
|
||||
};
|
||||
expect(await service.hasNotificationPermission(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
|
||||
// Denied in iOS Settings clears the cached grant.
|
||||
permissionsResponse = {
|
||||
'isEnabled': false,
|
||||
'isAlertEnabled': false,
|
||||
'isBadgeEnabled': false,
|
||||
'isSoundEnabled': false,
|
||||
'isProvisionalEnabled': false,
|
||||
};
|
||||
expect(await service.hasNotificationPermission(), isFalse);
|
||||
expect(service.canNotifyCacheForTest, isNull);
|
||||
expect(await service.ensureCanNotifyForTest(), isFalse);
|
||||
|
||||
permissionsResponse = null;
|
||||
expect(await service.hasNotificationPermission(), isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test('queries macOS checkPermissions and updates cache', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
|
||||
MacOSFlutterLocalNotificationsPlugin.registerWith();
|
||||
|
||||
Map<String, Object?>? permissionsResponse = {
|
||||
'isEnabled': true,
|
||||
'isAlertEnabled': true,
|
||||
'isBadgeEnabled': true,
|
||||
'isSoundEnabled': true,
|
||||
};
|
||||
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
if (call.method == 'initialize') return true;
|
||||
if (call.method == 'checkPermissions') return permissionsResponse;
|
||||
return null;
|
||||
});
|
||||
|
||||
expect(await service.hasNotificationPermission(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
|
||||
permissionsResponse = {
|
||||
'isEnabled': false,
|
||||
'isAlertEnabled': false,
|
||||
'isBadgeEnabled': false,
|
||||
'isSoundEnabled': false,
|
||||
};
|
||||
expect(await service.hasNotificationPermission(), isFalse);
|
||||
expect(service.canNotifyCacheForTest, isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'returns true on desktop platforms without an OS permission gate',
|
||||
() async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
|
||||
FlutterLocalNotificationsPlatform.instance =
|
||||
_NoopNotificationsPlatform();
|
||||
|
||||
expect(await service.hasNotificationPermission(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
test('requestPermissions updates and clears canNotify cache', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
AndroidFlutterLocalNotificationsPlugin.registerWith();
|
||||
|
||||
var grantResult = true;
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
if (call.method == 'initialize') return true;
|
||||
if (call.method == 'requestNotificationsPermission') {
|
||||
return grantResult;
|
||||
}
|
||||
if (call.method == 'areNotificationsEnabled') {
|
||||
return grantResult;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
expect(await service.requestPermissions(), isTrue);
|
||||
expect(service.canNotifyCacheForTest, isTrue);
|
||||
|
||||
grantResult = false;
|
||||
expect(await service.requestPermissions(), isFalse);
|
||||
expect(service.canNotifyCacheForTest, isNull);
|
||||
expect(await service.ensureCanNotifyForTest(), isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user