Reputation: 21
I've updated firebase messaging to the latest version and implement the new modifications but with firebase_messaging: ^8.0.0 and above I am not able to navigate to the respective screens when I push the notification icon if the application running in the background.
this is the code
class PushNotificationService
{
final FirebaseMessaging messaging = FirebaseMessaging.instance;
Future initialize(context) async{
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) => (Map<String, dynamic> message) async{
retrieveRideRequestInfo(getRideRequestId(message), context);
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) => (Map<String, dynamic> message) async {
retrieveRideRequestInfo(getRideRequestId(message), context);
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) => (Map<String, dynamic> message) async{
retrieveRideRequestInfo(getRideRequestId(message), context);
});
}
Future<String> getToken() async{
String token = await messaging.getToken();
//String token = await firebaseMessaging.getToken();
print("This is token :: ");
print(token);
driversRef.child(currentfirebaseUser.uid).child("token").set(token);
messaging .subscribeToTopic("alldrivers");
messaging .subscribeToTopic("allusers");
}
String getRideRequestId(Map<String, dynamic> message){
String rideRequestId = "";
if(Platform.isAndroid)
{
rideRequestId = message['data']['ride_request_id'];
}
else{
rideRequestId = message['ride_request_id'];
}
return rideRequestId;
}
void retrieveRideRequestInfo(String rideRequestId, BuildContext context){
newRequestsRef.child(rideRequestId).once().then((DataSnapshot dataSnapshot)
{
if (dataSnapshot.value != null) {
double pickUpLocationLat = double.parse(
dataSnapshot.value['pickup']['latitude'].toString());
double pickUpLocationLng = double.parse(
dataSnapshot.value['pickup']['longitude'].toString());
String pickUpAddress = dataSnapshot.value['pickup_address'].toString();
}
});
}
}
Run log:
D/FLTFireMsgReceiver( 8777): broadcast received for message
W/FLTFireMsgService( 8777): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
W/FirebaseMessaging( 8777): Unable to log event: analytics library is missing W/FirebaseMessaging( 8777): Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used. E/NotificationManager( 8777): notifyAsUser: tag=FCM-Notification:111236427, id=0, user=UserHandle{0}
Upvotes: 2
Views: 1896
Reputation: 6509
Had the same issue, with not working background Data FCM notifications. After investigation, I upgraded local notifications to the latest available and that fixed the problem!
dependencies:
flutter_local_notifications: ^9.7.0
Upvotes: 0
Reputation: 389
Handling messages whilst your application is in the background is a little different. Messages can be handled via the onBackgroundMessage handler. When received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.
There are a few things to keep in mind about your background message handler:
It must not be an anonymous function. It must be a top-level function (e.g. not a class method which requires initialization).
Example:
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());
}
Please go to official flutterfire docs for more information:
https://firebase.flutter.dev/docs/messaging/usage/
Upvotes: 1