Reputation: 35
_firebaseMessaging.*configure*( onMessage: (Map<String, dynamic> message) async {
print("\n\n on1Message: ${message.toString()}");
Map<String, dynamic> object = json.decode(
message['data']['notification'].toString());
print(
'\n\n Object==${message['data']}\n\n object===$object');
object['work'] = 'updateCount';
Stream<Map<String, dynamic>> stream =
Stream.value(object);
streamController.addStream(stream);
print("\n\n object ---> ${object}");
Error in Console Log---------------------------------------------------------------- 722:24: Error: The method 'configure' isn't defined for the class 'FirebaseMessaging'.
configure() method is not working after the update of the Package of Firebase Cloud Messaging. I tried different solution from the stack overflow but nothing works. What Should I do in my case.
Upvotes: 0
Views: 1026
Reputation: 789
The new FirebaseMessaging is a little bit different. Here are two interesting links: https://firebase.google.com/docs/flutter/setup?platform=android https://firebase.flutter.dev/docs/messaging/usage/
After adding firebase to the App, this is what I do (NotificationDetails is a class I wrote to show the details of the Notification. You can write you own class.):
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
'/': (context) => AppStarter(),
'/message': (context) => NotificationDetails(),
},
),
);
}
class AppStarter extends StatefulWidget{
@override
_AppStarterState createState() => _AppStarterState();
}
class _AppStarterState extends State<AppStarter>
{
FirebaseMessaging messaging = FirebaseMessaging.instance;
Future<void> showMeMyToken()
async {
var myToken = await messaging.getToken();
print("My Token is: " + myToken.toString());
}
@override
void initState() {
super.initState();
showMeMyToken();
FirebaseMessaging.instance.getInitialMessage().then((value) {
if(value != null)
{
Navigator.push(context,
MaterialPageRoute(
builder: (context){return NotificationDetails();},
settings: RouteSettings(arguments: value.data,),
),
);
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
print('Message on Foreground: ${message.notification}');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {return NotificationDetails();},
settings: RouteSettings(arguments: message.data,)
),
);
});
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Just a Test',
home: AppHome(),
);
}
}
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print("Handling a background message :-): ${message.data}");
//Here you can do what you want with the message :-)
}
Upvotes: 1