flutter-tutorials icon indicating copy to clipboard operation
flutter-tutorials copied to clipboard

Problem firebase messaging in new version flutter

Open AminAlizadeh-Dev opened this issue 4 years ago • 2 comments

Hi, in the firebase messaging version: 5.4.1 To get New from firebase, we did this: FirebaseMessaging _fcm = FirebaseMessaging (); But in the version of firebase messaging: 9.1.3 I have problem for New from firebase

thank you for your help.

AminAlizadeh-Dev avatar Apr 29 '21 12:04 AminAlizadeh-Dev

i have same problem

mraslamiii avatar May 10 '21 08:05 mraslamiii

This works for me, with Firebase Messaging v10.0.3

class PushNotificationService {
  final FirebaseMessaging _fcm = FirebaseMessaging.instance;
  final NavigationService _navigationService = locator<NavigationService>();

  Future initialise() async {
    NotificationSettings settings = await _fcm.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    if (settings.authorizationStatus == AuthorizationStatus.authorized) {
      print('User granted permission');
    } else if (settings.authorizationStatus ==
        AuthorizationStatus.provisional) {
      print('User granted provisional permission');
    } else {
      print('User declined or has not accepted permission');
    }

    // workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
    _fcm.getInitialMessage().then((RemoteMessage message) {
      print('getInitialMessage data: ${message.data}');
      _serialiseAndNavigate(message);
    });

    // onMessage: When the app is open and it receives a push notification
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("onMessage data: ${message.data}");
    });

    // replacement for onResume: When the app is in the background and opened directly from the push notification.
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('onMessageOpenedApp data: ${message.data}');
      _serialiseAndNavigate(message);
    });
  }

  void _serialiseAndNavigate(RemoteMessage message) {
  ...
  }
}

fransiskapw avatar Jul 22 '21 06:07 fransiskapw