mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 08:20:36 +00:00
Implement correct notification permissions for iOS and macOS
- Add timezone as direct dependency (per lint rules) - Update initialization to set all permissions to false - Add separate macOS initialization settings - Implement platform-specific permission requests for iOS and macOS - Follow recommended pattern from flutter_local_notifications docs Co-authored-by: dz0ny <239513+dz0ny@users.noreply.github.com>
This commit is contained in:
@@ -33,6 +33,9 @@ class NotificationService {
|
|||||||
'Notifications for incoming messages from contacts and channels';
|
'Notifications for incoming messages from contacts and channels';
|
||||||
|
|
||||||
/// Initialize notification service
|
/// Initialize notification service
|
||||||
|
/// Following best practices: set all permission requests to false during init,
|
||||||
|
/// then request permissions explicitly via requestPermissions() method.
|
||||||
|
/// This approach is recommended for iOS (all supported versions) and macOS 10.14+.
|
||||||
Future<void> initialize() async {
|
Future<void> initialize() async {
|
||||||
if (_isInitialized) return;
|
if (_isInitialized) return;
|
||||||
|
|
||||||
@@ -47,18 +50,27 @@ class NotificationService {
|
|||||||
'@mipmap/ic_launcher',
|
'@mipmap/ic_launcher',
|
||||||
);
|
);
|
||||||
|
|
||||||
// iOS initialization settings
|
// iOS initialization settings - set all permissions to false
|
||||||
final darwinSettings = DarwinInitializationSettings(
|
// Permissions will be requested later via requestPermissions()
|
||||||
requestAlertPermission: true,
|
const darwinSettings = DarwinInitializationSettings(
|
||||||
requestBadgePermission: true,
|
requestAlertPermission: false,
|
||||||
requestSoundPermission: true,
|
requestBadgePermission: false,
|
||||||
requestCriticalPermission: true, // For urgent SAR notifications
|
requestSoundPermission: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
// macOS initialization settings - set all permissions to false
|
||||||
|
// Permissions will be requested later via requestPermissions()
|
||||||
|
const macOSSettings = MacOSInitializationSettings(
|
||||||
|
requestAlertPermission: false,
|
||||||
|
requestBadgePermission: false,
|
||||||
|
requestSoundPermission: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Combined initialization settings
|
// Combined initialization settings
|
||||||
final initSettings = InitializationSettings(
|
final initSettings = InitializationSettings(
|
||||||
android: androidSettings,
|
android: androidSettings,
|
||||||
iOS: darwinSettings,
|
iOS: darwinSettings,
|
||||||
|
macOS: macOSSettings,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Initialize plugin
|
// Initialize plugin
|
||||||
@@ -67,7 +79,7 @@ class NotificationService {
|
|||||||
onDidReceiveNotificationResponse: _onNotificationResponse,
|
onDidReceiveNotificationResponse: _onNotificationResponse,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Request permissions
|
// Request permissions at the appropriate point (after initialization)
|
||||||
await _requestPermissions();
|
await _requestPermissions();
|
||||||
|
|
||||||
// Create notification channels (Android)
|
// Create notification channels (Android)
|
||||||
@@ -82,9 +94,13 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request notification permissions
|
/// Request notification permissions
|
||||||
|
/// Uses platform-specific implementations for iOS and macOS as recommended.
|
||||||
|
/// For iOS: Uses IOSFlutterLocalNotificationsPlugin.requestPermissions()
|
||||||
|
/// For macOS: Uses MacOSFlutterLocalNotificationsPlugin.requestPermissions()
|
||||||
|
/// For Android: Uses AndroidFlutterLocalNotificationsPlugin.requestNotificationsPermission()
|
||||||
Future<void> _requestPermissions() async {
|
Future<void> _requestPermissions() async {
|
||||||
try {
|
try {
|
||||||
// iOS permissions
|
// iOS permissions - request at appropriate point after initialization
|
||||||
final iosPlugin = _notificationsPlugin
|
final iosPlugin = _notificationsPlugin
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
IOSFlutterLocalNotificationsPlugin
|
IOSFlutterLocalNotificationsPlugin
|
||||||
@@ -104,6 +120,26 @@ class NotificationService {
|
|||||||
return; // Exit early if on iOS
|
return; // Exit early if on iOS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// macOS permissions - request at appropriate point after initialization
|
||||||
|
final macOSPlugin = _notificationsPlugin
|
||||||
|
.resolvePlatformSpecificImplementation<
|
||||||
|
MacOSFlutterLocalNotificationsPlugin
|
||||||
|
>();
|
||||||
|
if (macOSPlugin != null) {
|
||||||
|
final granted = await macOSPlugin.requestPermissions(
|
||||||
|
alert: true,
|
||||||
|
badge: true,
|
||||||
|
sound: true,
|
||||||
|
critical:
|
||||||
|
true, // Request critical alert permission for urgent SAR notifications
|
||||||
|
);
|
||||||
|
_permissionGranted = granted ?? false;
|
||||||
|
debugPrint(
|
||||||
|
'💻 [NotificationService] macOS permissions granted: $_permissionGranted',
|
||||||
|
);
|
||||||
|
return; // Exit early if on macOS
|
||||||
|
}
|
||||||
|
|
||||||
// Android 13+ permissions
|
// Android 13+ permissions
|
||||||
final androidPlugin = _notificationsPlugin
|
final androidPlugin = _notificationsPlugin
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ dependencies:
|
|||||||
|
|
||||||
# Notifications
|
# Notifications
|
||||||
flutter_local_notifications: ^19.5.0
|
flutter_local_notifications: ^19.5.0
|
||||||
|
timezone: ^0.9.0 # Required for scheduled notifications (direct dependency per lint rules)
|
||||||
|
|
||||||
# Vibration
|
# Vibration
|
||||||
vibration: ^3.1.4
|
vibration: ^3.1.4
|
||||||
|
|||||||
Reference in New Issue
Block a user