icon,权限
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
/// Notification Service - Handles local notifications
|
||||
class NotificationService {
|
||||
@@ -64,21 +66,40 @@ class NotificationService {
|
||||
// TODO: Navigate to appropriate screen if needed
|
||||
}
|
||||
|
||||
/// Request notification permissions (iOS only, Android auto-grants)
|
||||
/// Request notification permissions (iOS and Android 13+)
|
||||
Future<bool> requestPermissions() async {
|
||||
if (kIsWeb) return false;
|
||||
|
||||
try {
|
||||
final result = await _notifications
|
||||
.resolvePlatformSpecificImplementation<
|
||||
IOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
// Check if we're on Android or iOS
|
||||
if (Platform.isAndroid) {
|
||||
// Android 13+ requires runtime permission
|
||||
final status = await Permission.notification.request();
|
||||
|
||||
return result ?? true; // Android always returns true
|
||||
if (kDebugMode) {
|
||||
print('Android notification permission status: $status');
|
||||
}
|
||||
|
||||
return status.isGranted;
|
||||
} else if (Platform.isIOS) {
|
||||
// iOS permission request
|
||||
final result = await _notifications
|
||||
.resolvePlatformSpecificImplementation<
|
||||
IOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
if (kDebugMode) {
|
||||
print('iOS notification permission result: $result');
|
||||
}
|
||||
|
||||
return result ?? false;
|
||||
}
|
||||
|
||||
return true; // Other platforms
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Failed to request permissions: $e');
|
||||
@@ -87,6 +108,27 @@ class NotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if notification permission is granted
|
||||
Future<bool> hasPermission() async {
|
||||
if (kIsWeb) return false;
|
||||
|
||||
try {
|
||||
if (Platform.isAndroid) {
|
||||
final status = await Permission.notification.status;
|
||||
return status.isGranted;
|
||||
} else if (Platform.isIOS) {
|
||||
// For iOS, we can't easily check without requesting, so we assume granted after request
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Failed to check permission status: $e');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Show focus session completed notification
|
||||
Future<void> showFocusCompletedNotification({
|
||||
required int minutes,
|
||||
|
||||
Reference in New Issue
Block a user