notification page class
I use awesomenotification package and this is class
I made class for check permission and create notifications services
and show notifications function
class NotificationService {
static Future<void> initializeNotification() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelGroupKey: 'high_importance_channel',
channelKey: 'high_importance_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: const Color(0xFF9D50DD),
ledColor: Colors.white,
importance: NotificationImportance.High,
channelShowBadge: true,
onlyAlertOnce: true,
playSound: true,
criticalAlerts: true,
)
],
channelGroups: [
NotificationChannelGroup(
channelGroupKey: 'high_importance_channel_group',
channelGroupName: 'Group 1',
)
],
debug: true,
);
await AwesomeNotifications().isNotificationAllowed().then(
(isAllowed) async {
if (!isAllowed) {
await AwesomeNotifications().requestPermissionToSendNotifications();
}
},
);
await AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
onNotificationCreatedMethod: onNotificationCreatedMethod,
onNotificationDisplayedMethod: onNotificationDisplayedMethod,
onDismissActionReceivedMethod: onDismissActionReceivedMethod,
);
}
/// Use this method to detect when a new notification or a schedule is created
static Future<void> onNotificationCreatedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('onNotificationCreatedMethod');
}
/// Use this method to detect every time that a new notification is displayed
static Future<void> onNotificationDisplayedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('onNotificationDisplayedMethod');
}
/// Use this method to detect if the user dismissed a notification
static Future<void> onDismissActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('onDismissActionReceivedMethod');
}
/// Use this method to detect when the user taps on a notification or action button
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('onActionReceivedMethod');
final payload = receivedAction.payload ?? {};
if (payload["navigate"] == "true") {
MyApp.navigatorKey.currentState?.push(
MaterialPageRoute(
builder: (_) => const Salah(),
),
);
}
}
static Future<void> showNotification({
required final String title,
required final String body,
int? hour,
int? minute,
int? day,
int? month,
int? year,
}) async {
Random random = Random();
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: random.nextInt(1000) + 1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
),
actionButtons: actionButtons,
schedule: NotificationCalendar(
hour: hour,
minute: minute,
day: day,
month: month,
year: year,
allowWhileIdle: true,
timeZone: await AwesomeNotifications().getLocalTimeZoneIdentifier(),
repeats: true,
));
}
}
location and notification class
in this class i get latitude and longitude to pass them for adhan package to return time for each prayer time
class location extends StatefulWidget {
const location({super.key});
@override
State<location> createState() => _locationState();
}
// ignore: camel_case_types
class _locationState extends State<location> {
String _fullAddress = "اضغط هنا لتحديد الموقع الحالي";
double _latitude = 0.0;
double _longitude = 0.0;
@override
void initState() {
_schedulePrayerNotifications();
super.initState();
}
Future<void> _schedulePrayerNotifications() async {
final myCoordinates = Coordinates(
_latitude, _longitude); // Replace with your own location lat, lng.
final params = CalculationMethod.egyptian.getParameters();
params.madhab = Madhab.shafi;
final prayerTimes = PrayerTimes.today(myCoordinates, params);
await NotificationService.showNotification(
title: "صلاة الفجر",
body: "حان الوقت الأن لأداء صلاة الفجر بتوقيت $_fullAddress",
payload: {"navigate": "true"},
hour: prayerTimes.fajr.hour,
minute: prayerTimes.fajr.minute,
day: prayerTimes.fajr.day,
month: prayerTimes.fajr.month,
year: prayerTimes.fajr.year,
);
i see an issue while you are calling _schedulePrayerNotifications. let's try this below code, i hope it will save your times.