54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
class GeofenceNotificationsService {
|
|
final FlutterLocalNotificationsPlugin _plugin;
|
|
|
|
GeofenceNotificationsService({FlutterLocalNotificationsPlugin? plugin})
|
|
: _plugin = plugin ?? FlutterLocalNotificationsPlugin();
|
|
|
|
Future<void> initialize() async {
|
|
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) {
|
|
return;
|
|
}
|
|
|
|
const settings = InitializationSettings(
|
|
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
|
);
|
|
await _plugin.initialize(settings);
|
|
}
|
|
|
|
Future<bool> areNotificationsEnabled() async {
|
|
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
final android = _plugin
|
|
.resolvePlatformSpecificImplementation<
|
|
AndroidFlutterLocalNotificationsPlugin
|
|
>();
|
|
return await android?.areNotificationsEnabled() ?? true;
|
|
} catch (_) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
Future<bool> requestNotificationsPermission() async {
|
|
if (kIsWeb || defaultTargetPlatform != TargetPlatform.android) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
final android = _plugin
|
|
.resolvePlatformSpecificImplementation<
|
|
AndroidFlutterLocalNotificationsPlugin
|
|
>();
|
|
final granted = await android?.requestNotificationsPermission();
|
|
return granted ?? await areNotificationsEnabled();
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|