[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.

Fixed nextDates being stuck in the past when reinstalling the app from backup

Basically, if you reinstall the app, all notifications' dates will be updated to be in the future

Kasper (Jul 9, 2019, 1:29 AM +0200) 2f98fdba 57ba4d95

+47 -8
+26
android/app/src/main/java/space/kasper/notifier/MainActivity.java
··· 1 1 package space.kasper.notifier; 2 2 3 + import android.content.pm.PackageManager; 3 4 import android.os.Bundle; 5 + 4 6 import io.flutter.app.FlutterActivity; 7 + import io.flutter.plugin.common.MethodCall; 8 + import io.flutter.plugin.common.MethodChannel; 5 9 import io.flutter.plugins.GeneratedPluginRegistrant; 10 + import android.content.pm.PackageInfo; 6 11 7 12 public class MainActivity extends FlutterActivity { 13 + private static final String CHANNEL = "space.kasper.notifier/get_install_date"; 14 + 8 15 @Override 9 16 protected void onCreate(Bundle savedInstanceState) { 10 17 super.onCreate(savedInstanceState); 11 18 GeneratedPluginRegistrant.registerWith(this); 19 + 20 + new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( 21 + new MethodChannel.MethodCallHandler() { 22 + @Override 23 + public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { 24 + if (methodCall.method.equals("get_install_date")) { 25 + PackageInfo packageInfo = null; 26 + try { 27 + packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); 28 + long firstInstallTime = packageInfo.firstInstallTime; 29 + result.success(String.valueOf(firstInstallTime)); 30 + } catch (PackageManager.NameNotFoundException e) { 31 + e.printStackTrace(); 32 + result.error("PACKAGE NAME NOT FOUND", "Package name was not found", null); 33 + } 34 + } 35 + } 36 + } 37 + ); 12 38 } 13 39 }
+21 -8
lib/models/list.dart
··· 2 2 import 'dart:convert'; 3 3 import 'dart:core'; 4 4 import 'dart:ui'; 5 + import 'package:flutter/material.dart'; 6 + import 'package:flutter/services.dart'; 5 7 import 'package:scoped_model/scoped_model.dart'; 6 8 import 'package:shared_preferences/shared_preferences.dart'; 7 9 import 'package:flutter_local_notifications/flutter_local_notifications.dart'; ··· 110 112 int daysTillMonday = (7 - dateWeekday); 111 113 // set newDay to monday next week 112 114 newDay += daysTillMonday; 113 - 115 + 114 116 // skip weeks. If it's repeated every 2 week, add 7 days to newDay 115 117 newDay += (7 * repeatEvery) - 1; 116 118 ··· 118 120 int firstCheckedWeekday = weekdays.indexOf(true); 119 121 newDay += firstCheckedWeekday; 120 122 } 121 - 122 123 } else if (repeat == 'monthly') { 123 124 newMonth += repeatEvery; 124 125 } else if (repeat == 'yearly') { ··· 141 142 142 143 setNotifications() async { 143 144 print('[notifier] _setNotification'); 145 + 146 + const platform = MethodChannel('space.kasper.notifier/get_install_date'); 147 + String installDateInt = await platform.invokeMethod('get_install_date'); 148 + DateTime installDate = DateTime.fromMillisecondsSinceEpoch(int.parse(installDateInt)); 144 149 145 150 var androidPlatformChannelSpecifics = AndroidNotificationDetails( 146 151 'scheduled_notifications', ··· 178 183 // date to schedule notification to now 179 184 DateTime date = DateTime.fromMillisecondsSinceEpoch(notificationItem['date']); 180 185 // date to schedule notification to next time 181 - DateTime nextDate = getNextDate( 182 - date: date, 183 - repeat: notificationItem['repeat'], 184 - repeatEvery: notificationItem['repeatEvery'], 185 - weekdays: List<bool>.from(notificationItem['weekdays']), 186 - ); 186 + bool done = false; 187 + DateTime nextDate; 188 + while (!done) { 189 + nextDate = getNextDate( 190 + date: date, 191 + repeat: notificationItem['repeat'], 192 + repeatEvery: notificationItem['repeatEvery'], 193 + weekdays: List<bool>.from(notificationItem['weekdays']), 194 + ); 195 + // 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). 196 + if (installDate.millisecondsSinceEpoch > nextDate.millisecondsSinceEpoch) { 197 + done = true; 198 + } 199 + } 187 200 188 201 // 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. 189 202 if (notificationItem['date'] < DateTime.now().millisecondsSinceEpoch) {