[READ-ONLY] Mirror of https://github.com/probablykasper/notifier. Android app for scheduling notifications
android app flutter notifications
0

Configure Feed

Select the types of activity you want to include in your feed.

Notifs restored from backups will be disabled

+43 -22
+20 -1
lib/main.dart
··· 1 1 import 'package:flutter/material.dart'; 2 + import 'package:flutter/services.dart'; 3 + import 'package:notifier/models/list.dart'; 4 + import 'package:notifier/models/theme_model.dart'; 2 5 import 'package:notifier/views/app.dart'; 6 + import 'package:shared_preferences/shared_preferences.dart'; 7 + 8 + 3 9 4 - import 'models/theme_model.dart'; 10 + Future<void> checkIfRestored() async { 11 + ListModel listModel = ListModel(); 12 + 13 + // 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. 14 + await SharedPreferences.getInstance(); 15 + int lastBackgroundFecthDateMSSE = prefs.getInt('lastBackgroundFetchDate') ?? 0; 16 + 17 + const platform = MethodChannel('space.kasper.notifier/get_install_date'); 18 + String installDateMSSE = await platform.invokeMethod('get_install_date'); // MilliSecondsSinceEpoch 19 + if (int.parse(installDateMSSE) < lastBackgroundFecthDateMSSE) { 20 + listModel.disableAll(); 21 + } 22 + } 5 23 6 24 void main() async { 7 25 await ThemeModel.loadPrefs(); 26 + await checkIfRestored(); 8 27 runApp(App()); 9 28 }
+23 -21
lib/models/list.dart
··· 1 + import 'dart:async'; 1 2 import 'dart:collection'; 2 3 import 'dart:convert'; 3 4 import 'dart:core'; 4 5 import 'dart:ui'; 5 6 import 'package:flutter/material.dart'; 6 - import 'package:flutter/services.dart'; 7 7 import 'package:scoped_model/scoped_model.dart'; 8 8 import 'package:shared_preferences/shared_preferences.dart'; 9 9 import 'package:flutter_local_notifications/flutter_local_notifications.dart'; ··· 78 78 ), () async { 79 79 // This is the fetch-event callback. 80 80 print('[BackgroundFetch] Event received'); 81 + 82 + SharedPreferences prefs = await SharedPreferences.getInstance(); 83 + await prefs.setInt('lastBackgroundFetchDate', DateTime.now().millisecondsSinceEpoch); 84 + 85 + await setNotifications(appIsOpen: false); 81 86 // IMPORTANT: You must signal completion of your fetch task or the OS can punish your app 82 87 // for taking too long in the background. 83 - await setNotifications(appIsOpen: false); 84 88 BackgroundFetch.finish(); 85 89 }).then((int status) { 86 90 print('[BackgroundFetch] SUCCESS: $status'); ··· 142 146 if (id == pendingId) flutterLocalNotificationsPlugin.cancel(pendingNotification.id); 143 147 } 144 148 }); 149 + _save(); 150 + } 151 + 152 + disableAll() async { 153 + _notificationItems.forEach((id, notificationItem) { 154 + notificationItem['status'] = 'disabled'; 155 + }); 156 + _save(); 145 157 } 146 158 147 159 setNotifications({@required bool appIsOpen}) async { 148 160 print('[notifier] _setNotification'); 149 - 150 - const platform = MethodChannel('space.kasper.notifier/get_install_date'); 151 - String installDateInt = await platform.invokeMethod('get_install_date'); 152 - DateTime installDate = DateTime.fromMillisecondsSinceEpoch(int.parse(installDateInt)); 153 161 154 162 var androidPlatformChannelSpecifics = AndroidNotificationDetails( 155 163 'scheduled_notifications', ··· 184 192 if (!pendingNotificationItemIds.contains(notificationItem['id']) && 185 193 notificationItem['date'] < next48h && 186 194 notificationItem['status'] == 'enabled') { 187 - 188 195 // if the notification willBeDisabled, don't schedule a new notification 189 196 if (notificationItem['repeat'] == 'never' && notificationItem['willDisable'] == true) { 190 197 // if the notification date is in the past, disable the notification 191 198 if (notificationItem['date'] < DateTime.now().millisecondsSinceEpoch) { 192 199 notificationItem['status'] = 'disabled'; 200 + notificationItem['willDisable'] = false; 193 201 if (appIsOpen == true) rebuild(); 194 202 } 195 203 return; 196 204 } 197 - 205 + 198 206 // date to schedule notification to now 199 207 DateTime date = DateTime.fromMillisecondsSinceEpoch(notificationItem['date']); 200 208 // date to schedule notification to next time 201 - bool done = false; 202 209 DateTime nextDate; 203 - while (!done) { 204 - nextDate = getNextDate( 205 - date: date, 206 - repeat: notificationItem['repeat'], 207 - repeatEvery: notificationItem['repeatEvery'], 208 - weekdays: List<bool>.from(notificationItem['weekdays']), 209 - ); 210 - // 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). 211 - if (installDate.millisecondsSinceEpoch < nextDate.millisecondsSinceEpoch) { 212 - done = true; 213 - } 214 - } 210 + nextDate = getNextDate( 211 + date: date, 212 + repeat: notificationItem['repeat'], 213 + repeatEvery: notificationItem['repeatEvery'], 214 + weekdays: List<bool>.from(notificationItem['weekdays']), 215 + ); 215 216 216 217 // 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. 217 218 if (notificationItem['date'] < DateTime.now().millisecondsSinceEpoch) { ··· 238 239 } 239 240 } 240 241 }); 242 + _save(); 241 243 } 242 244 243 245 _save() async {