···11import 'package:flutter/material.dart';
22+import 'package:flutter/services.dart';
33+import 'package:notifier/models/list.dart';
44+import 'package:notifier/models/theme_model.dart';
25import 'package:notifier/views/app.dart';
66+import 'package:shared_preferences/shared_preferences.dart';
77+88+3944-import 'models/theme_model.dart';
1010+Future<void> checkIfRestored() async {
1111+ ListModel listModel = ListModel();
1212+1313+ // If the last time BackgroundFetch ran was before the app was installed, we'll disable all notifications, because that means the app data was restored from a backup.
1414+ await SharedPreferences.getInstance();
1515+ int lastBackgroundFecthDateMSSE = prefs.getInt('lastBackgroundFetchDate') ?? 0;
1616+1717+ const platform = MethodChannel('space.kasper.notifier/get_install_date');
1818+ String installDateMSSE = await platform.invokeMethod('get_install_date'); // MilliSecondsSinceEpoch
1919+ if (int.parse(installDateMSSE) < lastBackgroundFecthDateMSSE) {
2020+ listModel.disableAll();
2121+ }
2222+}
523624void main() async {
725 await ThemeModel.loadPrefs();
2626+ await checkIfRestored();
827 runApp(App());
928}
···11+import 'dart:async';
12import 'dart:collection';
23import 'dart:convert';
34import 'dart:core';
45import 'dart:ui';
56import 'package:flutter/material.dart';
66-import 'package:flutter/services.dart';
77import 'package:scoped_model/scoped_model.dart';
88import 'package:shared_preferences/shared_preferences.dart';
99import 'package:flutter_local_notifications/flutter_local_notifications.dart';
···7878 ), () async {
7979 // This is the fetch-event callback.
8080 print('[BackgroundFetch] Event received');
8181+8282+ SharedPreferences prefs = await SharedPreferences.getInstance();
8383+ await prefs.setInt('lastBackgroundFetchDate', DateTime.now().millisecondsSinceEpoch);
8484+8585+ await setNotifications(appIsOpen: false);
8186 // IMPORTANT: You must signal completion of your fetch task or the OS can punish your app
8287 // for taking too long in the background.
8383- await setNotifications(appIsOpen: false);
8488 BackgroundFetch.finish();
8589 }).then((int status) {
8690 print('[BackgroundFetch] SUCCESS: $status');
···142146 if (id == pendingId) flutterLocalNotificationsPlugin.cancel(pendingNotification.id);
143147 }
144148 });
149149+ _save();
150150+ }
151151+152152+ disableAll() async {
153153+ _notificationItems.forEach((id, notificationItem) {
154154+ notificationItem['status'] = 'disabled';
155155+ });
156156+ _save();
145157 }
146158147159 setNotifications({@required bool appIsOpen}) async {
148160 print('[notifier] _setNotification');
149149-150150- const platform = MethodChannel('space.kasper.notifier/get_install_date');
151151- String installDateInt = await platform.invokeMethod('get_install_date');
152152- DateTime installDate = DateTime.fromMillisecondsSinceEpoch(int.parse(installDateInt));
153161154162 var androidPlatformChannelSpecifics = AndroidNotificationDetails(
155163 'scheduled_notifications',
···184192 if (!pendingNotificationItemIds.contains(notificationItem['id']) &&
185193 notificationItem['date'] < next48h &&
186194 notificationItem['status'] == 'enabled') {
187187-188195 // if the notification willBeDisabled, don't schedule a new notification
189196 if (notificationItem['repeat'] == 'never' && notificationItem['willDisable'] == true) {
190197 // if the notification date is in the past, disable the notification
191198 if (notificationItem['date'] < DateTime.now().millisecondsSinceEpoch) {
192199 notificationItem['status'] = 'disabled';
200200+ notificationItem['willDisable'] = false;
193201 if (appIsOpen == true) rebuild();
194202 }
195203 return;
196204 }
197197-205205+198206 // date to schedule notification to now
199207 DateTime date = DateTime.fromMillisecondsSinceEpoch(notificationItem['date']);
200208 // date to schedule notification to next time
201201- bool done = false;
202209 DateTime nextDate;
203203- while (!done) {
204204- nextDate = getNextDate(
205205- date: date,
206206- repeat: notificationItem['repeat'],
207207- repeatEvery: notificationItem['repeatEvery'],
208208- weekdays: List<bool>.from(notificationItem['weekdays']),
209209- );
210210- // if the app was installed after nextDate, get skip over this date and get the next nextDate. This is for when you reinstall the app and get your old notifications loaded (e.g via google backup).
211211- if (installDate.millisecondsSinceEpoch < nextDate.millisecondsSinceEpoch) {
212212- done = true;
213213- }
214214- }
210210+ nextDate = getNextDate(
211211+ date: date,
212212+ repeat: notificationItem['repeat'],
213213+ repeatEvery: notificationItem['repeatEvery'],
214214+ weekdays: List<bool>.from(notificationItem['weekdays']),
215215+ );
215216216217 // Only set date to nextDate if the notification has already fired. The date needs to be the closest future notification date. When notifications are scheduled for the first time, their date should therefore not be updated.
217218 if (notificationItem['date'] < DateTime.now().millisecondsSinceEpoch) {
···238239 }
239240 }
240241 });
242242+ _save();
241243 }
242244243245 _save() async {