Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was following a tutorial for Firebase push notifications. I followed all the steps: Connecting app to Firebase, and importing packages (Firebase messaging and Firebase push notifications). I tried sending several notifications, but it did not work.

I know my app is connected to Firebase as I am receiving data from Cloud Firestore. Authentication also works and I am also able to write data to storage:

My main.dart:

Dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    // Added actual values
    options: const FirebaseOptions(
      apiKey: "have added this ",
      appId: "have added this",
      messagingSenderId: "have added this",
      projectId: "have added this",
      storageBucket: "have added this",
    ),
  );
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    FirebaseMessaging.instance
        .getInitialMessage(); /this helps code below work great

    FirebaseMessaging.onMessage.listen((message) {
      if (message.notification != null) {
        print(message.notification!.body);
        print(message.notification!.title);
      }
    }); 
    FirebaseMessaging.onMessageOpenedApp.listen((message) {
     
      final routeFromMessage = message.data['route'];

      print(routeFromMessage);
    });
  } 

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => GoogleSignInProvider(),
      child: OverlaySupport(
        child: MaterialApp(
            title: 'News_App_admin',
            theme: ThemeData(
              buttonTheme: const ButtonThemeData(minWidth: 80),
              primarySwatch: Colors.blue,
              secondaryHeaderColor: const Color.fromARGB(255, 75, 177, 78),
            ),
            home: // MyHomePage(title: 'Home page'),
                StreamBuilder(
              builder: (ctx, userSnapshot) {
                if (userSnapshot.hasError) {
                  Center(
                    child: Text('Unknown Error'),
                  );
                }
                if (userSnapshot.hasData) {
                 
                  return bottomNavigationBar();
                }
                return const Auth_Screen();
              },
              stream: FirebaseAuth.instance.authStateChanges(),
            ) 
  }
}


What I have tried:

I tried:

  1. Initializing Firebase and retrieving other data points (Authentication and Cloud Firestore); that is working so Firebase is connected
  2. SHA keys also attached to Firebase
  3. Initialized Firebase messaging

This much should have resulted in the notifications to appear, but that is not working.
Posted
Updated 4-Mar-22 5:35am
v2
Comments
wseng 6-Mar-22 3:52am    
did message.notification!.body; in onMessage.listen get printed?
Kavya Bhargava 6-Mar-22 4:59am    
No, it is not getting printed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900