Debug School

rakesh kumar
rakesh kumar

Posted on

How to redirect for forground and background message using firebase cloud messaging

redirection for foreground
redirection for background

redirection for foreground

  _notificationServices.firebaseInit(context);
Enter fullscreen mode Exit fullscreen mode
void initState() {
  super.initState();

  _notificationServices.requestNotificationPermission();
  _notificationServices.listenToTokenRefresh();

  _notificationServices.getDeviceToken().then((value) { print(value); });

  _notificationServices.initLocalNotifications();   // VERY IMPORTANT
  _notificationServices.firebaseInit(context);
  _notificationServices.handleNotificationClick(context);
}
Enter fullscreen mode Exit fullscreen mode

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);
  });
}
Enter fullscreen mode Exit fullscreen mode

redirection for background

  _notificationServices.handleNotificationClick(context);
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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 ?? "",
          ),
        ),
      );
    });
  }


Enter fullscreen mode Exit fullscreen mode
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)),
          ],
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)