0

当从终止状态点击通知时,我希望用户登陆到特定屏幕。我知道getInitialMessage()处理来自终止状态的通知,但它似乎没有导航到我想要的屏幕。

这是代码:

AndroidNotificationChannel channel;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
 final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");
 Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    RemoteNotification notification = message.notification;
   flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                icon: '@drawable/splash',
                playSound: true
              ),
            ));
            navigatorKey.currentState
                      .push(MaterialPageRoute(builder: (_) => OrdersScreen()));
}
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    channel = const AndroidNotificationChannel(
      'high_importance_channel', // id
      'High Importance Notifications', // title
      'This channel is used for important notifications.', // description
      importance: Importance.high,
    );
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
            ?.createNotificationChannel(channel);
    
     
      runApp(MyApp());
    }
    class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  void initState() {
         var initializationSettingsAndroid  = AndroidInitializationSettings('@drawable/splash');
    var initializationSettings = InitializationSettings(android:initializationSettingsAndroid);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);
     FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage message) {
          print('Inside get initial message'); 
      if (message != null) {
        print('Inside message not null'); 
          navigatorKey.currentState
                       .push(MaterialPageRoute(builder: (_) => OrdersScreen()));
      }
    });
}
}

如您所见,我想导航到订单屏幕,但我却登陆了登录页面。我错过了什么?当通知从后台状态或前台状态被点击时,它工作正常,但不是从终止状态。

4

0 回答 0