19
import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

import 'package:google_maps_flutter/google_maps_flutter.dart';    

class PushNotificationService
{
  final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

  Future initialize(context) async
   {
    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        retrieveRideRequestInfo(getRideRequestId(message), context);
       },
       onLaunch: (Map<String, dynamic> message) async {
         retrieveRideRequestInfo(getRideRequestId(message), context);
       },
       onResume: (Map<String, dynamic> message) async {
        retrieveRideRequestInfo(getRideRequestId(message), context);
      },
     );
    }

Manifest:

    <intent-filter>
        <action android:name="FLUTTER_NOTIFICATION_CLICK" />
        <category android:name="android.intent.category.DEFAULT" 
        />
    </intent-filter>

Error: The class 'FirebaseMessaging' doesn't have a default constructor. Try using one of the named constructors defined in 'FirebaseMessaging'.

1
  • 1
    Which version of Firebase messaging are you using? Commented Mar 27, 2021 at 9:03

4 Answers 4

73

Try FirebaseMessaging.instance instead of FirebaseMessaging(), it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah that's what I did. Thanks
@FranklinPremkumar Please mark it as correct answer then
2

try to add add dependencies firebase_core 1.4.0

import 'package:firebase_core/firebase_core.dart';

    @override
      void initState() {
        // TODO: implement initState
        super.initState();
        Firebase.initializeApp();
      }
     
      onPressed: () async {
           String token = await FirebaseMessaging.instance.getToken();
           print(token);
                    
        },

Comments

-1

Use FirebaseStorage.instanceFor instead of the default constructor

Comments

-2

Try FirebaseMessaging.instance

1 Comment

@Coder - This is an exact duplicate of a three year old answer, while it's confirmed this was indeed the solution to the author's problem, in this particular case duplicating an answer without any additional helpful insight is NOT helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.