ente/lib/services/notification_service.dart

91 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2023-06-24 06:02:26 +00:00
import "package:shared_preferences/shared_preferences.dart";
class NotificationService {
static final NotificationService instance =
NotificationService._privateConstructor();
2023-06-24 06:02:26 +00:00
static const String keyGrantedNotificationPermission =
"notification_permission_granted";
2023-06-25 12:34:00 +00:00
static const String keyShouldShowNotificationsForSharedPhotos =
"notifications_enabled_shared_photos";
NotificationService._privateConstructor();
2023-06-24 06:02:26 +00:00
late SharedPreferences _preferences;
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> init() async {
2023-06-24 06:02:26 +00:00
_preferences = await SharedPreferences.getInstance();
const androidSettings = AndroidInitializationSettings('notification_icon');
const iosSettings = DarwinInitializationSettings();
2022-07-04 06:02:17 +00:00
const InitializationSettings initializationSettings =
InitializationSettings(
2023-06-24 06:02:26 +00:00
android: androidSettings,
iOS: iosSettings,
);
2023-06-24 05:39:58 +00:00
await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
2023-06-25 12:29:35 +00:00
if (!hasGrantedPermissions()) {
await requestPermissions();
}
}
Future<void> requestPermissions() async {
if (Platform.isIOS) {
2023-06-24 06:02:26 +00:00
final result = await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
sound: true,
alert: true,
);
2023-06-25 12:29:35 +00:00
if (result != null) {
_preferences.setBool(keyGrantedNotificationPermission, result);
2023-06-24 06:02:26 +00:00
}
}
}
2023-06-25 12:29:35 +00:00
bool hasGrantedPermissions() {
if (Platform.isAndroid) {
return true;
}
2023-06-24 06:02:26 +00:00
final result = _preferences.getBool(keyGrantedNotificationPermission);
return result ?? false;
}
2023-06-25 12:34:00 +00:00
bool shouldShowNotificationsForSharedPhotos() {
final result =
_preferences.getBool(keyShouldShowNotificationsForSharedPhotos);
2023-06-25 12:29:35 +00:00
return result ?? true;
}
2023-06-25 12:34:00 +00:00
Future<void> setShouldShowNotificationsForSharedPhotos(bool value) {
return _preferences.setBool(
keyShouldShowNotificationsForSharedPhotos,
value,
);
2023-06-25 12:29:35 +00:00
}
Future<void> showNotification(String title, String message) async {
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'io.ente.photos',
'ente',
2022-05-18 10:08:13 +00:00
channelDescription: 'ente alerts',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
);
const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(
2022-06-11 08:23:52 +00:00
0,
title,
message,
platformChannelSpecifics,
);
}
}