Sound Conception
Sound Conception

Reputation: 5421

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?

Upvotes: 0

Views: 197

Answers (1)

DF_
DF_

Reputation: 3983

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;
    }
  }

Upvotes: 1

Related Questions