Flutter Firebase Messaging 触发 onLaunch

Flutter Firebase Messaging trigger onLaunch

所以我有一个问题,因为我没有完全理解某些东西。所以我写了一个简单的应用程序来尝试一些东西,所以我通过 Firebase Cloud Messaging 从 Firebase 发送消息。我使用 firebase_messaging flutter 插件(7.0.3 版本)。它只是做了一件简单的事情来获取消息并导航到另一个页面。我的问题是以下一个,onMessage 和 onResume 功能完美运行,但是当应用程序终止并且我单击通知时,应用程序打开但没有任何反应。所以我阅读了文档,发现了这个 第三行说,数据丢失了……这是否意味着我丢失了有效负载中的所有数据? 如果有帮助,我的代码就在这里

 static final NavigationService _navigationService =
      loc.Locator.locator<NavigationService>();
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Future<void> reqPermission() async {
    bool permission = true;
    if (Platform.isIOS) {
      permission = await _firebaseMessaging.requestNotificationPermissions();
    }
    if (permission) {
      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print(message);
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('onLaunch $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onResume: (Map<String, dynamic> message) async {
          print('onResume $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onBackgroundMessage: _myBackgroundHandler,
      );
    }
  }

您可以使用 fcm 发送通知和数据,它支持终止时的通知,但不支持数据。您可以在通知中发送 body 和标题,所以如果您需要基本通知(只有标题和 body)。您可以使用通知发送它。 如果您需要更多,可以将其与 flutter_local_notifications 包一起使用 在启动时。 https://pub.dev/packages/flutter_local_notifications

这是使用代币的方法

_firebaseMessaging.onTokenRefresh.listen((newToken) {
      User _currentUser = FirebaseAuth.instance.currentUser;
      FirebaseFirestore.instance
          .doc("tokens/" + _currentUser.uid)
          .set({"token": newToken});
    });

你可以这样推。

  Future<bool> sendNotification(
      {@required Map<String, dynamic> messageMap,
      @required AppUser appUser,
      @required String token}) async {
    String url = "https://fcm.googleapis.com/fcm/send";
    String _firebaseKey ="<your key>"

    Map<String, String> headers = {
      "Content-type": "application/json",
      "Authorization": "key=$_firebaseKey"
    };
    String json =
        '{ "to" : "$token", "data" : { "message" : "${messageMap["message"]}", "sendBy": "${appUser.name}", "messageType": "${messageMap["messageType"]}", "sendById" : "${appUser.userId}" } }';
    http.post(url, headers: headers, body: json);
    return true;
  }

FCM 配置

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        //getNotificationMessage(message["data"]["message"]);
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message); // Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onResume: (Map<String, dynamic> message) async {
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );
  }

这是后台消息处理程序

Future<void> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  if (message.containsKey('data')) {
    // Handle data message
    final dynamic data = message['data'];
    if(message["data"]["messageType"]=="text"){
      NotificationHandler.showNotification(message);// Flutter Local Notification method
    } else{
      NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
    }

  }

  return Future.value();
}

这就是我在项目中的做法

我最近在 GitHub 上问了这个问题,请参阅:https://github.com/FirebaseExtended/flutterfire/issues/5098#issuecomment-783444919

That was true for the old version and it required a native workaround. The latest non-nullsafety version firebase_messaging: ^8.0.0-dev.14 will work from a terminated state (unless you force-quit the app on android). The updated documentation is at https://firebase.flutter.dev/docs/messaging/usage#receiving-messages