I am working on scheduled notifications. When I click on the press button I am getting a message in debug console saying "Notification Scheduled for TimeOfDay(19:10)" but there is no notification popping up on my emulator screen. A notification should pop up at the time that I entered using the pick button
This is the code
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
NotificationService().initNotification();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter demo',
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TimeOfDay _timeOfDay = const TimeOfDay(hour: 8, minute: 30);
void _timePicker() {
showTimePicker(context: context, initialTime: TimeOfDay.now())
.then((value) {
if (value != null) {
setState(() {
_timeOfDay = value;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
const SizedBox(
height: 30,
),
MaterialButton(
onPressed: _timePicker,
child: const Text(
'PICK',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
),
),
ElevatedButton(
child: const Text('press'),
onPressed: () {
debugPrint('Notification Scheduled for $_timeOfDay');
NotificationService().scheduleNotification(
title: 'Schedule notification',
body: '$_timeOfDay',
scheduledNotificationDateTime: _timeOfDay);
},
),
],
),
),
);
}
}
class NotificationService {
final FlutterLocalNotificationsPlugin notificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
AndroidInitializationSettings initializationSettingsAndroid =
const AndroidInitializationSettings('panda');
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
);
await notificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) async {});
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'channel id',
'channel name',
importance: Importance.max,
);
await notificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
AndroidNotificationDetails notificationDetails() {
return const AndroidNotificationDetails('channel id', 'channel name',
importance: Importance.max);
}
NotificationDetails buildNotificationDetails() {
final androidDetails = notificationDetails();
return NotificationDetails(android: androidDetails);
}
Future<void> showNotification(
{int id = 0, String? title, String? body, String? payload}) async {
return notificationsPlugin.show(id, title, body,
NotificationDetails(android: await notificationDetails()));
}
Future<void> scheduleNotification({
int id = 0,
String? title,
String? body,
String? payload,
required TimeOfDay scheduledNotificationDateTime,
}) async {
final now = DateTime.now();
var scheduledDateTime = DateTime(
now.year,
now.month,
now.day,
scheduledNotificationDateTime.hour,
scheduledNotificationDateTime.minute,
);
// Ensure that the scheduledDateTime is in the future
if (scheduledDateTime.isBefore(now)) {
// If it's in the past, add one day
scheduledDateTime = scheduledDateTime.add(const Duration(days: 1));
}
return notificationsPlugin.zonedSchedule(
id,
title,
body,
TZDateTime.from(scheduledDateTime, tz.local),
buildNotificationDetails(),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
}