【问题标题】:Flutter push notification FCM not navigating on launchFlutter 推送通知 FCM 在启动时无法导航
【发布时间】:2020-12-22 08:12:54
【问题描述】:

有一个颤振应用程序。正在使用 FCM 发送推送通知。

所需的行为是在单击通知时,它应该进入显示通知详细信息的屏幕。该屏幕已实现。

在应用程序处于后台时单击通知,单击将进入所需的屏幕,但当应用程序在单击时启动时,它打开的是主屏幕而不是所需的通知屏幕。

推送通知服务代码。

class PushNotificationsManager {

  PushNotificationsManager._();

  factory PushNotificationsManager() => _instance;

  static final PushNotificationsManager _instance = PushNotificationsManager._();

  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  bool _initialized = false;

  Future<void> init() async {
    if (!_initialized) {
      // For iOS request permission first.
      _firebaseMessaging.requestNotificationPermissions();
      //configure
      _firebaseMessaging.configure(
        //Triggered if a message is received whilst the app is in foreground
        onMessage: (Map<String, dynamic> message) async {
          print("onMessage: $message");
//          _navigateToItemDetail(message);
//          _showItemDialog(message);
        },
        //this line causes hot reload error
//        onBackgroundMessage: myBackgroundMessageHandler,
        //Triggered if a message is received if the app was terminated
        onLaunch: (Map<String, dynamic> message) async {
          print("onLaunch: $message");
          _navigateToItemDetail(message);
        },
        //Triggered if a message is received whilst the app is in background
        onResume: (Map<String, dynamic> message) async {
          print("onResume: $message");
          _navigateToItemDetail(message);
        },
      );

      // For testing purposes print the Firebase Messaging token
      String token = await _firebaseMessaging.getToken();
      print("FirebaseMessaging token: $token");

      _initialized = true;
    }
  }


  void _navigateToItemDetail(Map<String, dynamic> message) {
    print("Navigating to notification screen");
    bool isValid = message["data"]["title"] != null && message["data"]["body"] != null;

    if(!isValid) return;

    navigatorKey.currentState.pushNamedAndRemoveUntil(notificationScreenRoute, (route) => false, arguments: message);
   // navigatorKey.currentState.pushNamed(notificationScreenRoute, arguments: message);
  }
}

main.dart -- 这里我正在初始化推送通知服务

//global navigator key,used where buildContext is not available (in business logic)
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  //init Hive
  await initHiveDb();

  PushNotificationsManager _pushNotificationManager = PushNotificationsManager();
  _pushNotificationManager.init();

  // get the dynamic link service
  final DynamicLinkService _dynamicLinkService = DynamicLinkService();
  // call handle dynamic links
  await _dynamicLinkService.handleDynamicLinks();

  //run app, restrict orientaton to portrait up
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      systemNavigationBarColor: kMaroonColor
  ));
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
    runApp(
      MaterialApp(
        debugShowCheckedModeBanner: true,
        theme: (appLightTheme).copyWith(
            //textTheme: GoogleFonts.latoTextTheme()
        ),
        onGenerateRoute: RouteGenerator.generateRoute,
        navigatorKey: navigatorKey,
        home: App()
      )
    );
  });
}

【问题讨论】:

    标签: firebase flutter push-notification firebase-cloud-messaging


    【解决方案1】:

    当您的应用未运行时,扩展 FlutterFragmentActivityMainActivity 也未运行。因此,由于您在启动应用程序时 FlutterActivity 尚未准备好,因此您无法在 Flutter 页面之间导航,直到它被加载

    【讨论】:

    • 好的。那么这是什么转机。如何确保导航发生?
    • 视情况而定。一种方法是使用系统通知。您发送给用户的每个通知,您都可以将其副本保存在数据库中,其中包含seen 之类的字段,默认情况下保持值false,并在导航到特定页面时更改为true。我的意思是类似 facebook 通知
    • 我只想在点击时显示一次屏幕。我不想存储通知。
    • 这只有在应用程序运行时才有可能。否则,将加载默认主页。这能回答你的问题吗?
    猜你喜欢
    • 2023-02-14
    • 2020-10-10
    • 2020-10-14
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2021-06-06
    • 2021-11-27
    • 2021-01-12
    相关资源
    最近更新 更多