Skip to content

Commit

Permalink
local notifications integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Spyy004 committed May 10, 2023
1 parent 804fff4 commit 514a257
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 10 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ flutter {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:31.5.0')
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'androidx.window:window:1.0.0'
implementation 'androidx.window:window-java:1.0.0'
}
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:label="lifelist"
android:name="${applicationName}"
Expand Down
Binary file added android/app/src/main/res/drawable/app_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion lib/constants/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const DONE = 'Done';
const CREATE = 'Create';
const FEEDBACK_EMAIL = '[email protected]';
const FEEDBACK = 'Feedback';
const NOTIFICATION_CHANNEL_ID = 'streak1';
const NOTIFICATION_CHANNEL_NAME = 'streak';
const NOTIFICATION_CHANNEL_DESCRIPTION = 'Notification for streak reminder';
const APP_LOGO = 'app_logo';
int currIndex = 0;
String appVersion = '0.0.0';
var routes = <String, WidgetBuilder>{
Expand Down Expand Up @@ -124,7 +128,7 @@ Map<String, BucketCategory> stringToBucketCategory = {
};

Map<BucketCategory, String> bucketCategoryToString = {
BucketCategory.travel: 'Travel',
BucketCategory.travel: 'Travel',
BucketCategory.finance: 'Finance',
BucketCategory.adventure: 'Adventure',
BucketCategory.career: 'Career',
Expand Down
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:lifelist/constants/index.dart';
import 'package:lifelist/services/index.dart';
import 'package:lifelist/services/notificationservice.dart';
import 'package:lifelist/services/pdfservice.dart';
import 'package:lifelist/services/singletemplateservice.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await NotificationService().init();
await Firebase.initializeApp();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
appVersion = packageInfo.version;
dbService = DBService();
Expand Down
13 changes: 6 additions & 7 deletions lib/services/bucketlistservice.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:lifelist/controllers/global_controller.dart';
import 'package:lifelist/models/bucket.dart';
import 'package:lifelist/services/firebaseservice.dart';

class BucketListService extends ChangeNotifier {
List<Bucket?> buckets = [];
Expand Down Expand Up @@ -43,15 +42,15 @@ class BucketListService extends ChangeNotifier {

measureTimeLeft() {
filteredBuckets.forEach((element) {
if(element!.isCompleted){
element.timeLeft = "Done";
return;
}
if(element.bucketScope == BucketScope.onetime){
if (element!.isCompleted) {
element.timeLeft = "Done";
return;
}
if (element.bucketScope == BucketScope.onetime) {
if (element.deadline.isBefore(DateTime.now())) {
element.timeLeft = "Expired";
return;
}
}
}
Duration difference = element.deadline.difference(DateTime.now());
if (difference.inDays > 0) {
Expand Down
49 changes: 49 additions & 0 deletions lib/services/notificationservice.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:lifelist/constants/index.dart';
import 'package:lifelist/utils/utils.dart';

class NotificationService {
static final NotificationService _notificationService =
NotificationService._internal();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
factory NotificationService() {
return _notificationService;
}

NotificationService._internal();

Future<void> init() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings(APP_LOGO);

const InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid, iOS: null, macOS: null);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
bool? isPermissionGranted = await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission();
if (isPermissionGranted == true) {
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
}

void scheduleNotification(String title, String body, int id, bool isCompleted,
DateTime deadline) async {
final timeToNotify = getNotificationScheduleTime(deadline, isCompleted);
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
timeToNotify,
const NotificationDetails(
android: AndroidNotificationDetails(
NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
channelDescription: NOTIFICATION_CHANNEL_DESCRIPTION)),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
}
10 changes: 10 additions & 0 deletions lib/services/singlebucketservice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:fluttertoast/fluttertoast.dart';
import 'package:lifelist/constants/index.dart';
import 'package:lifelist/models/index.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:lifelist/services/notificationservice.dart';
import '../controllers/global_controller.dart';

class BucketService extends ChangeNotifier {
Expand Down Expand Up @@ -199,6 +200,15 @@ class BucketService extends ChangeNotifier {
activeSingleBucket.tasks = activeSingleBucket.tasks.toList();
activeSingleBucket.tasks.addAll(newTasksId);
await editBucketinDB(activeSingleBucket);
String notificationTitle = 'STREAK WILL END SOON! ⌛️';
String notificationBody =
"Time to finish ${activeSingleBucket.name} bucket for today ✓";
NotificationService().scheduleNotification(
notificationTitle,
notificationBody,
activeSingleBucket.id!,
activeSingleBucket.isCompleted,
activeSingleBucket.deadline);
activeBucketTasks = [];
loader = false;
notifyListeners();
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

int daysBetween(DateTime froms, DateTime to) {
froms = DateTime(froms.year, froms.month, froms.day);
to = DateTime(to.year, to.month, to.day);
return (to.difference(froms).inHours / 24).round();
}

tz.TZDateTime getNotificationScheduleTime(DateTime deadline, bool isCompleted) {
final timeZoneName = tz.TimeZone.UTC.abbreviation;
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation(timeZoneName));
tz.Location location = tz.getLocation(timeZoneName);
final scheduledDate = tz.TZDateTime.from(deadline, location);
tz.TZDateTime timeToNotify = isCompleted
? scheduledDate.add(const Duration(hours: 23))
: scheduledDate.subtract(const Duration(hours: 1));
return timeToNotify;
}
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
dbus:
dependency: transitive
description:
name: dbus
sha256: "6f07cba3f7b3448d42d015bfd3d53fe12e5b36da2423f23838efc1d5fb31a263"
url: "https://pub.dev"
source: hosted
version: "0.7.8"
dependencies:
dependency: "direct main"
description:
Expand Down Expand Up @@ -374,6 +382,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter_local_notifications:
dependency: "direct main"
description:
name: flutter_local_notifications
sha256: "2876372952b65ca7f684e698eba22bda1cf581fa071dd30ba2f01900f507d0d1"
url: "https://pub.dev"
source: hosted
version: "14.0.0+1"
flutter_local_notifications_linux:
dependency: transitive
description:
name: flutter_local_notifications_linux
sha256: "909bb95de05a2e793503a2437146285a2f600cd0b3f826e26b870a334d8586d7"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
sha256: "63235c42de5b6c99846969a27ad0209c401e6b77b0498939813725b5791c107c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
flutter_localizations:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -645,6 +677,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.1.0"
platform:
dependency: transitive
description:
name: platform
sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -669,6 +709,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
process:
dependency: transitive
description:
name: process
sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09"
url: "https://pub.dev"
source: hosted
version: "4.2.4"
provider:
dependency: "direct main"
description:
Expand Down Expand Up @@ -802,6 +850,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.3"
timezone:
dependency: "direct main"
description:
name: timezone
sha256: "1cfd8ddc2d1cfd836bc93e67b9be88c3adaeca6f40a00ca999104c30693cdca0"
url: "https://pub.dev"
source: hosted
version: "0.9.2"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -922,6 +978,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.3"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: ee1505df1426458f7f60aac270645098d318a8b4766d85fde75f76f2e21807d1
url: "https://pub.dev"
source: hosted
version: "1.0.0"
xml:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ dependencies:
flutter_email_sender: ^5.2.0
cloud_firestore: ^4.5.3
firebase_core: ^2.10.0
flutter_local_notifications: ^14.0.0+1
timezone: ^0.9.2


dev_dependencies:
Expand Down

0 comments on commit 514a257

Please sign in to comment.