Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

flutter - Firebase messaging onResume, onLaunch or onBackgroundMessage are never called

I am using Firebase Cloud Messaging package for flutter. I want that whenever a user taps on the received notification, the flutter app opens and navigate to a particular screen depending on the data sent by fcm.

But I am not able to achieve desired behaviour when either app is in the background or app is closed since onResume and onLaunch handler are never called. I have tried a number of solution like creating notification channel , using package like flutter_local_notifications etc. But nothing worked for me.

Here is my FirebaseMessagingService class to handle fcm related tasks

class FirebaseMessagingService {
  final FirebaseMessaging _fcm = FirebaseMessaging();
  final RouterService _routerService = locator<RouterService>();
  final LocalStorageService _localStorage = locator<LocalStorageService>();
  Future initialise() async {
    if (Platform.isIOS) {
      _fcm.requestNotificationPermissions(IosNotificationSettings());
    }
    _fcm.configure(
        onMessage: (Map<String, dynamic> message) async {
          Map<String, dynamic> arguments = {};

          Map<String, dynamic> data = message['data'];
          if (data.containsKey('argument')) {
            if (data.containsKey('value')) {
              arguments[data['argument']] = data['value'];
            }
          }
          _routerService.navigationKey.currentState
              .pushReplacementNamed('${data['route']}', arguments: arguments);
        },
        onLaunch: (Map<String, dynamic> message) async {
          Map<String, dynamic> arguments = {};
          Map<String, dynamic> data = message['data'];
          if (data.containsKey('argument')) {
            if (data.containsKey('value')) {
              arguments[data['argument']] = data['value'];
            }
          }
          _routerService.navigationKey.currentState
              .pushReplacementNamed('${data['route']}', arguments: arguments);
        },
        onResume: (Map<String, dynamic> message) async {
          Map<String, dynamic> arguments = {};

          Map<String, dynamic> data = message['data'];
          if (data.containsKey('argument')) {
            if (data.containsKey('value')) {
              arguments[data['argument']] = data['value'];
            }
          }
          _routerService.navigationKey.currentState
              .pushReplacementNamed('${data['route']}', arguments: arguments);
        },
        onBackgroundMessage: _myBackgroundMessageHandler);
  }

  static Future<dynamic> _myBackgroundMessageHandler(
      Map<String, dynamic> message) async {
    final RouterService _routerService = locator<RouterService>();
    if (message.containsKey('data')) {
      // Handle data message
      Map<String, dynamic> arguments = {};
      Map<String, dynamic> data = message['data'];
      if (data.containsKey('argument')) {
        if (data.containsKey('value')) {
          arguments[data['argument']] = data['value'];
        }
      }
      _routerService.navigationKey.currentState
          .pushReplacementNamed('${data['route']}', arguments: arguments);
    }

    if (message.containsKey('notification')) {
      // Handle notification message
      final dynamic notification = message['notification'];
    }
  }
}

Can anyone help me out with the issue??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...