how to use firebase messaging 9.0 version latest versiom - flutter

i was tring to integrate new firebase messaging version dependancy
firebaseMessaging.configure(
onLaunch: (Map<String, dynamic> msg) {
print(" onLaunch called ${(msg)}");
},
onResume: (Map<String, dynamic> msg) {
print(" onResume called ${(msg)}");
},
onMessage: (Map<String, dynamic> msg) {
showNotification(msg);
print(" onMessage called ${(msg)}");
},
i am getting eroor lines in this point .configure
this is dart analysis error
The method 'configure' isn't defined for the type 'FirebaseMessaging'.
Try correcting the name to the name of an existing method, or defining a method named 'configure'.

there are some changes in the library, delete what you mentioned and use this code instead within your initState function:
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
try {
final data = message.data;
print(message.notification);
print(message.notification.title);
} catch (e) {
print(e);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
try {
print('onResume: $message');
final data = message.data;
print(message.notification);
} catch (e) {
print(e);
}
});
you should read the changeLog

You need to import firebase messaging.

Related

The method 'configure' isn't defined for the type 'FirebaseMessaging'. Can anyone retype my code correctly? Thanks

Push Notification code.
I don't understand how to implement the new method of implementing a notification in flutter using firebase
import 'dart:io' show Platform;
import 'package:google_maps_flutter/google_maps_flutter.dart';
class PushNotificationService
{
final firebaseMessaging = FirebaseMessaging.instance;
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);
},
);
}
Seems like you are using the old method. Here is the updated version. Link for your references
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
showNotification(notification);});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("onMessageOpenedApp: $message");});
FirebaseMessaging.onBackgroundMessage((RemoteMessage message) {
print("onBackgroundMessage: $message");});

when I wrote this code in my project but the version of firebase_messaging is ^9.1.2 so anyone can tell me what i can change to run correctly

Future initialize(context) async {
FirebaseMessaging.instance.getNotificationSettings(
FirebaseMessaging.onMessage.listen((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 {
print("onResume: $message");
getRideRequestId(message);
retrieveRideRequestInfo(getRideRequestId(message), context);
},
);
}
Instead of on Message use :
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
for background message
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
Note background message is handled in separate isolate if you are using firebase you have to initialise it first to use it.
Reference:
Foreground:https://firebase.flutter.dev/docs/messaging/usage#foreground-messages
Background:https://firebase.flutter.dev/docs/messaging/usage#background-messages

Flutter how to access provider.of(context) in FCM backgroundHandler static method?

I have successfully setup background notifications and tested it using postman and all is good.
Now I need to access Provider.of(context) in my backgroundHandler which must be a static method where there is no context.
All I need to do is to perform an action according to the data in the background notification.
Here is my code for initializing FCM (I do it in Splash screen)
Future<void> initFcm() async {
_firebaseMessaging.configure(
onBackgroundMessage: myBackgroundMessageHandler,
onMessage: (msg) async {
print('this is ONMESSAGE $msg');
},
onLaunch: (msg) async {
print('ON LAUNCH');
},
onResume: (msg) async {
print('ON RESUME');
},
);
// For testing purposes print the Firebase Messaging token
deviceToken = await _firebaseMessaging.getToken();
print("FirebaseMessaging token: $deviceToken");
}
static Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) async {
print(message);
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
final orderId = data['order_id'];
//THE PROBLEM IS THAT HERE I DON'T HAVE CONTEXT (coz static method)
Provider.of<Orders>(context, listen: false).setBackgroundStatus(orderId);
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
// Or do other work.
}
My question is how can I handle tasks in provider in my backgroundHandler which is a static method that doesn't have context?

Flutter FirebaseMessaging setup

In initState() of the _PushMessagingExampleState class in the example app for Flutter firebase_messaging 6.0.13, _firebaseMessaging.configure is called first, followed by _firebaseMessaging.requestNotificationPermissions.
However, in the firebase_messaging 6.0.13 API reference for the FirebaseMessaging class, the first thing it states is:
Your app should call requestNotificationPermissions first and then
register handlers for incoming messages with configure.
Which is correct?
I'm guessing the API reference is correct and the example app could be improved?
Technically it doesn't matter but I have it set up as request permissions first, then configure. As an example of a working app:
configureFcm() {
if (!isConfigured) {
if (Platform.isIOS) {
_fcm.onIosSettingsRegistered.listen((IosNotificationSettings data) {
});
_fcm.requestNotificationPermissions(IosNotificationSettings(sound: true, badge: true, alert: true));
}
_fcm.configure(
// in foreground
onMessage: (Map<String, dynamic> message) async {
_handleNotification(message);
},
// onBackgroundMessage: (Map<String, dynamic> message) async {
// print('on background message $message');
// },
// in background
onResume: (Map<String, dynamic> message) async {
_handleNotification(message);
},
// terminated
onLaunch: (Map<String, dynamic> message) async {
_handleNotification(message);
},
);
isConfigured = true;
}
}

Broadcast fcm notification throughout the app in flutter

The question is I want to receive FCM notification from different view because I have notification bell icon which is updated whenever the notification is received.I have Profile View, Landing View, Dashboard View and others. So I have badge in there which needs update. However, I cannot receive the broadcast either using Stream or Notifier. The only option I have is to add a bulky line to each of these pages. i.e
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
setState(() {
counter++;
});
_newNotification = true;
_showNotificationWithDefaultSound(message.toString());
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
setState(() {
counter++;
});
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
setState(() {
counter++;
});
print("onResume: $message");
},
);
And also I need to add couple of functions to get it working.So my question is how do you receive it with the minimum possible lines.As I have tried Stream broadcast which only works for single page and I cant listen it from another page.Anyone if they had this issue please share.All in the internet I am getting this solution and which is not applicable for me .
void initState() {
super.initState();
print("Creating a sample stream...");
Stream<String> stream = new Stream.fromFuture(getData());
print("Created the stream");
stream.listen((data) {
print("DataReceived: "+data);
}, onDone: () {
print("Task Done");
}, onError: (error) {
print("Some Error");
});
print("code controller is here");
}
From this link which is good example