2023-02-21 14:28:52 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
|
class NotificationPermissionNotifier extends StateNotifier<PermissionStatus> {
|
2024-01-27 18:14:32 +02:00
|
|
|
NotificationPermissionNotifier()
|
|
|
|
: super(
|
|
|
|
Platform.isAndroid
|
|
|
|
? PermissionStatus.granted
|
|
|
|
: PermissionStatus.restricted,
|
|
|
|
) {
|
2023-02-21 14:28:52 +02:00
|
|
|
// Sets the initial state
|
|
|
|
getNotificationPermission().then((p) => state = p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Requests the notification permission
|
|
|
|
/// Note: In Android, this is always granted
|
|
|
|
Future<PermissionStatus> requestNotificationPermission() async {
|
2024-01-27 18:14:32 +02:00
|
|
|
final permission = await Permission.notification.request();
|
|
|
|
state = permission;
|
|
|
|
return permission;
|
2023-02-21 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the user has the permission or not
|
|
|
|
/// Note: In Android, this is always true
|
|
|
|
Future<bool> hasNotificationPermission() {
|
|
|
|
return Permission.notification.isGranted;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<PermissionStatus> getNotificationPermission() async {
|
|
|
|
final status = await Permission.notification.status;
|
|
|
|
state = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Either the permission was granted already or else ask for the permission
|
|
|
|
Future<bool> hasOrAskForNotificationPermission() {
|
|
|
|
return requestNotificationPermission().then((p) => p.isGranted);
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 18:14:32 +02:00
|
|
|
|
|
|
|
final notificationPermissionProvider =
|
|
|
|
StateNotifierProvider<NotificationPermissionNotifier, PermissionStatus>(
|
|
|
|
(ref) => NotificationPermissionNotifier(),
|
|
|
|
);
|