Why isn’t it working on Android when I tap the notification that was pushed from Firebase in Flutter, but it’s working fine on iOS?


I’ve been trying to find a solution for a few days, but haven’t had any luck yet. I’ve already checked the permissions in AndroidManifest.xml and tested on both real devices and emulators/simulators, but it’s still not working on Android. It only works on iOS.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (kReleaseMode) {
    debugPrint = (String? message, {int? wrapWidth}) {};
  }

  //Firebase Core
  FirebaseApp app = await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform);

  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
    // Handle notification when the app is in the background, and the user taps on it
    await _showNotificationDialog(
      context: navigatorkey.currentState!.overlay!.context,
      title: message.notification?.title ?? "Notification",
      body: message.notification?.body ?? "Notification body",
    );
  });

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  

  runApp(const MyApp());
}

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  if (message.notification != null) {
    // Handle notification when the app is terminated
    await _showNotificationDialog(
      context: navigatorkey.currentState!.overlay!.context,
      title: message.notification?.title ?? "",
      body: message.notification?.body ?? "",
    );
  }
}

Future<void> _showNotificationDialog({
  required BuildContext context,
  required String title,
  required String body,
}) async {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text(title!),
        content: Text(body!),
        actions: <Widget>[
          TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: Text('OK'),
          ),
        ],
      );
    },
  );
}

I want my code to perform the following actions:

  1. When a notification is pushed from Firebase, display a dialog box
    when the user taps on the notification.
  2. This functionality is currently working on iOS but not on Android.
    Please assist with troubleshooting the Android issue.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img