首页 > 解决方案 > Flutter Web中收到后台通知时如何执行flutter代码?

问题描述

在 Flutter Web 中,当收到 Firebase 通知并且应用程序在前面时,我确实执行了“onMessage”处理程序。但是,当应用程序在后台时,我确实看到了带有消息预览的 Chrome 通知,但应用程序中没有执行任何操作。我找不到让服务人员“ping”正在运行的应用程序的方法,甚至无法简单地运行独立的 Flutter 代码来设置某些内容,SharedPreferences以便用户在打开应用程序时收到完整的消息。

例如,这项工作在 Android 中按预期工作。

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  if (message.notification != null) {
    SharedPreferences.getInstance().then((SharedPreferences p) {
      p.setString("notification", message.notification.body.toString());
    });
  }
}


//[... initialization code : ...]
        String notif = prefs.getString("notification");

        if (notif != null) {
          showSnack(notif, duration: Duration(days: 365));
          prefs.remove("notification");
         }

        FirebaseMessaging.onBackgroundMessage(
            _firebaseMessagingBackgroundHandler);
        FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          if (message.notification != null) {
            showSnack(message.notification.body.toString(),
                duration: Duration(days: 365));
          }
        });

标签: flutterweb

解决方案


推荐阅读