ente/mobile/lib/services/notification_service.dart

130 lines
4 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import "package:logging/logging.dart";
import "package:photos/services/remote_sync_service.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;
2023-06-26 06:24:34 +00:00
final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
final _logger = Logger("NotificationService");
Future<void> init(
void Function(
NotificationResponse notificationResponse,
)
onNotificationTapped,
SharedPreferences preferences,
) async {
_preferences = preferences;
2023-06-24 06:02:26 +00:00
const androidSettings = AndroidInitializationSettings('notification_icon');
const iosSettings = DarwinInitializationSettings(
requestAlertPermission: false,
requestSoundPermission: false,
requestBadgePermission: false,
requestCriticalPermission: false,
);
2022-07-04 06:02:17 +00:00
const InitializationSettings initializationSettings =
InitializationSettings(
2023-06-24 06:02:26 +00:00
android: androidSettings,
iOS: iosSettings,
);
await _notificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: onNotificationTapped,
);
final launchDetails =
await _notificationsPlugin.getNotificationAppLaunchDetails();
if (launchDetails != null &&
launchDetails.didNotificationLaunchApp &&
launchDetails.notificationResponse != null) {
onNotificationTapped(launchDetails.notificationResponse!);
}
if (!hasGrantedPermissions() &&
RemoteSyncService.instance.isFirstRemoteSyncDone()) {
2023-06-25 12:29:35 +00:00
await requestPermissions();
}
}
Future<void> requestPermissions() async {
2023-06-26 06:24:22 +00:00
bool? result;
2023-06-25 12:29:35 +00:00
if (Platform.isIOS) {
2023-06-26 06:24:34 +00:00
result = await _notificationsPlugin
2023-06-24 06:02:26 +00:00
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
sound: true,
alert: true,
);
} else {
2023-06-26 06:24:34 +00:00
result = await _notificationsPlugin
2023-06-26 06:24:22 +00:00
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission();
}
if (result != null) {
2023-12-16 21:04:26 +00:00
await _preferences.setBool(keyGrantedNotificationPermission, result);
2023-06-24 06:02:26 +00:00
}
}
2023-06-25 12:29:35 +00:00
bool hasGrantedPermissions() {
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, {
String channelID = "io.ente.photos",
String channelName = "ente",
String payload = "ente://home",
}) async {
_logger.info(
"Showing notification with: $title, $message, $channelID, $channelName, $payload",
);
final androidSpecs = AndroidNotificationDetails(
channelID,
channelName,
2022-05-18 10:08:13 +00:00
channelDescription: 'ente alerts',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
);
final iosSpecs = DarwinNotificationDetails(threadIdentifier: channelID);
final platformChannelSpecs =
NotificationDetails(android: androidSpecs, iOS: iosSpecs);
2023-06-26 06:24:34 +00:00
await _notificationsPlugin.show(
channelName.hashCode,
2022-06-11 08:23:52 +00:00
title,
message,
platformChannelSpecs,
payload: payload,
2022-06-11 08:23:52 +00:00
);
}
}