Skip to content

Commit

Permalink
Merge pull request #41 from IamMuuo/dev
Browse files Browse the repository at this point in the history
Notifications Request
  • Loading branch information
IamMuuo authored Jan 8, 2024
2 parents f16fbfb + 3842449 commit ae5242c
Show file tree
Hide file tree
Showing 8 changed files with 509 additions and 74 deletions.
Binary file added android/app/src/main/res/drawable/app_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion lib/controllers/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SettingsController extends GetxController {

@override
void onInit() async {
settings = await appDB.get("settings");
settings = await appDB.get("settings") ?? {};
showGPA.value = settings["show_gpa"] ?? true;
showProfilePic.value = settings["show_profile_pic"] ?? true;
showExamTimeTable.value = settings["show_exam_timetable"] ?? true;
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:academia/controllers/settings_controller.dart';
import 'package:academia/models/courses.dart';
import 'package:academia/models/schedule.dart';
import 'package:academia/models/user.dart';
import 'package:academia/notifications/notification_service.dart';
import 'package:academia/pages/home_page.dart';
import 'package:academia/pages/intro_page.dart';
import 'package:academia/themes/theme.dart';
Expand All @@ -12,6 +13,8 @@ import 'package:get/get.dart';
import 'package:magnet/magnet.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await NotificationService().initNotifications();
// Init flutter
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
Expand All @@ -28,6 +31,7 @@ void main() async {

// Init settings controller
Get.put(SettingsController());
NotificationService().showNotification();

runApp(
GetMaterialApp(
Expand Down
99 changes: 99 additions & 0 deletions lib/notifications/notification_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//////////////////////////////////////////////////////////////////////////////////
// File Notification Service
// Author: Erick Muuo
// Email: [email protected]
// Description: Read dont touch !!!!
//////////////////////////////////////////////////////////////////////////////////
// WARNING: NO SUPPORT FOR IOS, LINUX, MAC, WEB or WINDOWS
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

// Hands off dont touch this Code
// Signed Erick Muuo
class NotificationService {
// Create a singleton instance
static final NotificationService _notificationService =
NotificationService._internal();

factory NotificationService() {
return _notificationService;
}

NotificationService._internal();

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

final AndroidNotificationDetails _androidNotificationDetails =
const AndroidNotificationDetails("channel ID", "channel name",
playSound: true,
priority: Priority.high,
importance: Importance.high);

late NotificationDetails platformChannelSpecifics;

// notifications initialize called only once
Future<void> initNotifications() async {
AndroidInitializationSettings initializationSettingsAndroid =
const AndroidInitializationSettings("app_icon");

final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
);

platformChannelSpecifics =
NotificationDetails(android: _androidNotificationDetails);

tz.initializeTimeZones();

await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}

// Show a notification instantly
Future<void> showNotification({
String title = "Academia",
String body = "Hi from Academia",
int id = 0,
String payload = "Greeting",
}) async {
await flutterLocalNotificationsPlugin
.show(id, title, body, platformChannelSpecifics, payload: payload);
}

// Schedule Notifications
Future<void> scheduleNotification(
int id,
String title,
String body,
Duration duration,
) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.now(tz.local).add(duration),
platformChannelSpecifics,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}

// Cancels a notification specified by ID
Future<void> cancelNotifications(int id) async {
await flutterLocalNotificationsPlugin.cancel(id);
}

// Cancels all previously scheduled notifications
Future<void> cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
}

Future selectNotification(String payload) async {
Get.snackbar("Notification", payload);
}
Loading

0 comments on commit ae5242c

Please sign in to comment.