Show a notification on Android when storage limit is exceeded

This commit is contained in:
Vishnu Mohandas 2021-05-21 04:53:29 +05:30
parent 1a20fd3779
commit 537abae771
6 changed files with 84 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 901 B

View file

@ -12,6 +12,7 @@ import 'package:photos/db/upload_locks_db.dart';
import 'package:photos/services/billing_service.dart';
import 'package:photos/services/collections_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/ui/app_lock.dart';
import 'package:photos/ui/home_widget.dart';
@ -118,6 +119,7 @@ Future<void> _init(bool isBackground) async {
_logger.info("Initializing...");
InAppPurchaseConnection.enablePendingPurchases();
CryptoUtil.init();
await NotificationService.instance.init();
await Network.instance.init();
await Configuration.instance.init();
await BillingService.instance.init();

View 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);
}
}

View file

@ -4,6 +4,7 @@ import 'package:computer/computer.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/core/errors.dart';
import 'package:photos/core/event_bus.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/models/file_type.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/notification_service.dart';
import 'package:photos/utils/diff_fetcher.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/utils/file_sync_util.dart';
@ -44,6 +46,8 @@ class SyncService {
static const kDbUpdationTimeKey = "db_updation_time";
static const kHasCompletedFirstImportKey = "has_completed_firstImport";
static const kHasGrantedPermissionsKey = "has_granted_permissions";
static const kLastStorageLimitExceededNotificationPushTime =
"last_storage_limit_exceeded_notification_push_time";
static const kLastBackgroundUploadDetectedTime =
"last_background_upload_detected_time";
static const kDiffLimit = 2500;
@ -114,6 +118,7 @@ class SyncService {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
error: NoActiveSubscriptionError()));
} on StorageLimitExceededError {
_showStorageLimitExceededNotification();
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error,
error: StorageLimitExceededError()));
} on UnauthorizedError {
@ -449,4 +454,15 @@ class SyncService {
"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");
}
}
}

View file

@ -265,6 +265,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct dev"
description:
@ -770,6 +784,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
timezone:
dependency: transitive
description:
name: timezone
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.0"
typed_data:
dependency: transitive
description:

View file

@ -79,6 +79,7 @@ dependencies:
move_to_background: ^1.0.2
loading_animations: ^2.1.0
dots_indicator: ^2.0.0
flutter_local_notifications: ^5.0.0+4
dev_dependencies:
flutter_test: