redirection for foreground
redirection for background
redirection for foreground
_notificationServices.firebaseInit(context);
void initState() {
super.initState();
_notificationServices.requestNotificationPermission();
_notificationServices.listenToTokenRefresh();
_notificationServices.getDeviceToken().then((value) { print(value); });
_notificationServices.initLocalNotifications(); // VERY IMPORTANT
_notificationServices.firebaseInit(context);
_notificationServices.handleNotificationClick(context);
}
R:\firebase\firebase\lib\notification_services.dart
/// FORGOUND MESSAGE HANDLER
void firebaseInit(BuildContext context) {
FirebaseMessaging.onMessage.listen((message) {
print(message.notification!.title.toString());
print(message.notification!.body.toString());
handleMessage(context, message);
showNotification(message);
});
}
redirection for background
_notificationServices.handleNotificationClick(context);
R:\firebase\firebase\lib\home_screen.dart
void initState() {
super.initState();
_notificationServices.requestNotificationPermission();
_notificationServices.listenToTokenRefresh();
_notificationServices.getDeviceToken().then((value) { print(value); });
_notificationServices.initLocalNotifications(); // VERY IMPORTANT
_notificationServices.firebaseInit(context);
_notificationServices.handleNotificationClick(context);
}
R:\firebase\firebase\lib\notification_services.dart
/// HANDLE NOTIFICATION CLICK (Terminated & Background)
void handleNotificationClick(BuildContext context) {
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("Notification clicked!");
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => NotificationClickScreen(
title: message.notification?.title ?? "Clicked Notification",
body: message.notification?.body ?? "",
),
),
);
});
}
class NotificationClickScreen extends StatelessWidget {
final String title;
final String body;
const NotificationClickScreen({
super.key,
required this.title,
required this.body,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Notification Details")),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Title: $title",
style:
const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Text("Body: $body", style: const TextStyle(fontSize: 18)),
],
),
),
);
}
}
Top comments (0)