Show a notification on Android when storage limit is exceeded
This commit is contained in:
parent
1a20fd3779
commit
537abae771
6 changed files with 84 additions and 0 deletions
BIN
android/app/src/main/res/drawable/notification_icon.png
Normal file
BIN
android/app/src/main/res/drawable/notification_icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 901 B |
|
@ -12,6 +12,7 @@ import 'package:photos/db/upload_locks_db.dart';
|
||||||
import 'package:photos/services/billing_service.dart';
|
import 'package:photos/services/billing_service.dart';
|
||||||
import 'package:photos/services/collections_service.dart';
|
import 'package:photos/services/collections_service.dart';
|
||||||
import 'package:photos/services/memories_service.dart';
|
import 'package:photos/services/memories_service.dart';
|
||||||
|
import 'package:photos/services/notification_service.dart';
|
||||||
import 'package:photos/services/sync_service.dart';
|
import 'package:photos/services/sync_service.dart';
|
||||||
import 'package:photos/ui/app_lock.dart';
|
import 'package:photos/ui/app_lock.dart';
|
||||||
import 'package:photos/ui/home_widget.dart';
|
import 'package:photos/ui/home_widget.dart';
|
||||||
|
@ -118,6 +119,7 @@ Future<void> _init(bool isBackground) async {
|
||||||
_logger.info("Initializing...");
|
_logger.info("Initializing...");
|
||||||
InAppPurchaseConnection.enablePendingPurchases();
|
InAppPurchaseConnection.enablePendingPurchases();
|
||||||
CryptoUtil.init();
|
CryptoUtil.init();
|
||||||
|
await NotificationService.instance.init();
|
||||||
await Network.instance.init();
|
await Network.instance.init();
|
||||||
await Configuration.instance.init();
|
await Configuration.instance.init();
|
||||||
await BillingService.instance.init();
|
await BillingService.instance.init();
|
||||||
|
|
44
lib/services/notification_service.dart
Normal file
44
lib/services/notification_service.dart
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
|
||||||
|
class NotificationService {
|
||||||
|
static final NotificationService instance =
|
||||||
|
NotificationService._privateConstructor();
|
||||||
|
|
||||||
|
NotificationService._privateConstructor();
|
||||||
|
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
|
||||||
|
FlutterLocalNotificationsPlugin();
|
||||||
|
|
||||||
|
Future<void> init() async {
|
||||||
|
const AndroidInitializationSettings initializationSettingsAndroid =
|
||||||
|
AndroidInitializationSettings('notification_icon');
|
||||||
|
final InitializationSettings initializationSettings =
|
||||||
|
InitializationSettings(
|
||||||
|
android: initializationSettingsAndroid,
|
||||||
|
);
|
||||||
|
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
|
||||||
|
onSelectNotification: selectNotification);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future selectNotification(String payload) async {}
|
||||||
|
|
||||||
|
Future<void> showNotification(String title, String message) async {
|
||||||
|
if (!Platform.isAndroid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const AndroidNotificationDetails androidPlatformChannelSpecifics =
|
||||||
|
AndroidNotificationDetails(
|
||||||
|
'io.ente.photos',
|
||||||
|
'ente',
|
||||||
|
'ente alerts',
|
||||||
|
importance: Importance.max,
|
||||||
|
priority: Priority.high,
|
||||||
|
showWhen: false,
|
||||||
|
);
|
||||||
|
const NotificationDetails platformChannelSpecifics =
|
||||||
|
NotificationDetails(android: androidPlatformChannelSpecifics);
|
||||||
|
await _flutterLocalNotificationsPlugin.show(
|
||||||
|
0, title, message, platformChannelSpecifics);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import 'package:computer/computer.dart';
|
||||||
import 'package:connectivity/connectivity.dart';
|
import 'package:connectivity/connectivity.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:photos/core/constants.dart';
|
||||||
import 'package:photos/core/errors.dart';
|
import 'package:photos/core/errors.dart';
|
||||||
import 'package:photos/core/event_bus.dart';
|
import 'package:photos/core/event_bus.dart';
|
||||||
import 'package:photos/core/network.dart';
|
import 'package:photos/core/network.dart';
|
||||||
|
@ -15,6 +16,7 @@ import 'package:photos/events/subscription_purchased_event.dart';
|
||||||
import 'package:photos/events/trigger_logout_event.dart';
|
import 'package:photos/events/trigger_logout_event.dart';
|
||||||
import 'package:photos/models/file_type.dart';
|
import 'package:photos/models/file_type.dart';
|
||||||
import 'package:photos/services/collections_service.dart';
|
import 'package:photos/services/collections_service.dart';
|
||||||
|
import 'package:photos/services/notification_service.dart';
|
||||||
import 'package:photos/utils/diff_fetcher.dart';
|
import 'package:photos/utils/diff_fetcher.dart';
|
||||||
import 'package:photo_manager/photo_manager.dart';
|
import 'package:photo_manager/photo_manager.dart';
|
||||||
import 'package:photos/utils/file_sync_util.dart';
|
import 'package:photos/utils/file_sync_util.dart';
|
||||||
|
@ -44,6 +46,8 @@ class SyncService {
|
||||||
static const kDbUpdationTimeKey = "db_updation_time";
|
static const kDbUpdationTimeKey = "db_updation_time";
|
||||||
static const kHasCompletedFirstImportKey = "has_completed_firstImport";
|
static const kHasCompletedFirstImportKey = "has_completed_firstImport";
|
||||||
static const kHasGrantedPermissionsKey = "has_granted_permissions";
|
static const kHasGrantedPermissionsKey = "has_granted_permissions";
|
||||||
|
static const kLastStorageLimitExceededNotificationPushTime =
|
||||||
|
"last_storage_limit_exceeded_notification_push_time";
|
||||||
static const kLastBackgroundUploadDetectedTime =
|
static const kLastBackgroundUploadDetectedTime =
|
||||||
"last_background_upload_detected_time";
|
"last_background_upload_detected_time";
|
||||||
static const kDiffLimit = 2500;
|
static const kDiffLimit = 2500;
|
||||||
|
@ -114,6 +118,7 @@ class SyncService {
|
||||||
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
|
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
|
||||||
error: NoActiveSubscriptionError()));
|
error: NoActiveSubscriptionError()));
|
||||||
} on StorageLimitExceededError {
|
} on StorageLimitExceededError {
|
||||||
|
_showStorageLimitExceededNotification();
|
||||||
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
|
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
|
||||||
error: StorageLimitExceededError()));
|
error: StorageLimitExceededError()));
|
||||||
} on UnauthorizedError {
|
} on UnauthorizedError {
|
||||||
|
@ -449,4 +454,15 @@ class SyncService {
|
||||||
"fileIDs": fileIDs,
|
"fileIDs": fileIDs,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _showStorageLimitExceededNotification() async {
|
||||||
|
final lastNotificationShownTime =
|
||||||
|
_prefs.getInt(kLastStorageLimitExceededNotificationPushTime) ?? 0;
|
||||||
|
final now = DateTime.now().microsecondsSinceEpoch;
|
||||||
|
if ((now - lastNotificationShownTime) > MICRO_SECONDS_IN_DAY) {
|
||||||
|
await _prefs.setInt(kLastStorageLimitExceededNotificationPushTime, now);
|
||||||
|
NotificationService.instance.showNotification(
|
||||||
|
"storage limit exceeded", "sorry, we had to pause your backups");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
21
pubspec.lock
21
pubspec.lock
|
@ -265,6 +265,20 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.9.0"
|
version: "0.9.0"
|
||||||
|
flutter_local_notifications:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.0+4"
|
||||||
|
flutter_local_notifications_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications_platform_interface
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.0"
|
||||||
flutter_native_splash:
|
flutter_native_splash:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
|
@ -770,6 +784,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.19"
|
version: "0.2.19"
|
||||||
|
timezone:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: timezone
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.7.0"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -79,6 +79,7 @@ dependencies:
|
||||||
move_to_background: ^1.0.2
|
move_to_background: ^1.0.2
|
||||||
loading_animations: ^2.1.0
|
loading_animations: ^2.1.0
|
||||||
dots_indicator: ^2.0.0
|
dots_indicator: ^2.0.0
|
||||||
|
flutter_local_notifications: ^5.0.0+4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in a new issue