i am new in flutter app. i have used flutter local push notification as the following example link, video. it's working fine but while clicking the notification the page doesn't navigate and reload.
how to navigate and reload the page while clicking the notification?
I have also created second screen (this is navigate page) but in this code selectNotification functionality not working.
//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()),
);
}
}
//main.dart
import 'secondscreen.dart';
import 'package:flutter/material.dart';
import 'package:tasker/notification_service.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
NotificationService().init();
// NotificationService().requestIOSPermissions(); //
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
accentColor: Colors.indigo.shade900,
appBarTheme: AppBarTheme(color: Colors.indigo.shade900)),
home: MyHomePage(title: 'Flutter Local Notifications'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
NotificationService _notificationService = NotificationService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RaisedButton(
child: Text('Show Notification'),
padding: const EdgeInsets.all(10),
onPressed: () async {
await _notificationService.showNotifications();
},
),
SizedBox(height: 3),
RaisedButton(
child: Text('Schedule Notification'),
padding: const EdgeInsets.all(10),
onPressed: () async {
await _notificationService.scheduleNotifications();
},
),
SizedBox(height: 3),
RaisedButton(
child: Text('Cancel Notification'),
padding: const EdgeInsets.all(10),
onPressed: () async {
await _notificationService.cancelNotifications(0);
},
),
SizedBox(height: 3),
RaisedButton(
child: Text('Cancel All Notifications'),
padding: const EdgeInsets.all(10),
onPressed: () async {
await _notificationService.cancelAllNotifications();
},
),
SizedBox(height: 3),
SizedBox(height: 3),
RaisedButton(
child: Text('naviagte page'),
padding: const EdgeInsets.all(10),
onPressed: () {
// Navigator.push(
// context,
// MaterialPageRoute(builder: (context) => SecondScreen()),
// );
},
),
],
),
)));
}
}