Hi iam new user in flutter app. I need while clicking the notification navigate and reload the page. can you please suggest how to navigate and reload the page while clicking the notification. i have used flutter local push notification as the following example link(https://blog.logrocket.com/implementing-local-notifications-in-flutter/) working fine but while clicking the notication the page does’t navigate and reload.
I have also created secondscreen(this is navigate page) but in this code selectNotification functionality not working. please check below code and give me a solution.
//notification_service.dart
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'secondscreen.dart';
import 'main.dart';
class NotificationService {
//NotificationService a singleton object
static final NotificationService _notificationService =
NotificationService._internal();
factory NotificationService() {
return _notificationService;
}
NotificationService._internal();
static const channelId = '123';
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> init() async {
final AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
);
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: null);
tz.initializeTimeZones();
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);
}
AndroidNotificationDetails _androidNotificationDetails =
AndroidNotificationDetails(
'channel ID',
'channel name',
'channel description',
playSound: true,
priority: Priority.high,
importance: Importance.high,
);
Future<void> showNotifications() async {
await flutterLocalNotificationsPlugin.show(
0,
"Notification sundar",
"This is the Notification Body!",
NotificationDetails(android: _androidNotificationDetails),
);
}
Future<void> scheduleNotifications() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
"Notification Title",
"This is the Notification Body!",
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
NotificationDetails(android: _androidNotificationDetails),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
Future<void> cancelNotifications(int id) async {
await flutterLocalNotificationsPlugin.cancel(id);
}
Future<void> cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
}
Future selectNotification(String payload) async {
//handle your logic here
print('String');
if (payload != null) {
print('sundar');
BuildContext context;
Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => SecondScreen()),
);
}
}