首页 > 解决方案 > Flutter 小部件未在通知单击时呈现

问题描述

我正在使用firebase_messagingflutter_local_notifications库来通知一切工作正常。但是在 Android 中,如果从应用程序堆栈中删除应用程序(从最近的 Activity 中删除幻灯片)而不是收到通知并单击它,则小部件无法正确呈现,按钮导航、应用程序栏、文本正在消失。

 @override
 Widget build(BuildContext context) {

return LayoutBuilder(builder: (_, constraints) {
  return OrientationBuilder(builder: (_, orientation) {
    SizeConfig().init(constraints, orientation);
    return MultiBlocProvider(
        providers: [
          BlocProvider(create: (_)=>_mainAppBarBloc),
          BlocProvider(create: (_) => _hadyahBloc),
   
        ],
        child:MultiBlocListener(
            listeners: [
              BlocListener<HadyahBloc,HadyahState>(
                listener: (_,state){
                  state.whenPartial(
                    getLanguage: (s){
                      var locale = _hadyahBloc.changeLanguage(s.language);
                      setLocale(locale);
                    },
                  );
                },
              ),
            ],
            child: MaterialApp(
              key: _mKey,
              navigatorObservers: [
                sl<FirebaseAnalyticsService>().getObserver(),
              ],
              builder: (context,_){
                return Scaffold(
                  key: _scaffoldkey,
                  body: Container(
                    color: Colors.white,
                    child: ExtendedNavigator<Router>(
                      router: Router(),
                      key: navigatorKey,
                      initialRoute: Routes.screenNavigatorHandler,
                    ),
                  ),
                );
              },
              locale: _locale,
              supportedLocales: [
                Locale('en','GB'),//english united kingdom
                Locale('ar','SA')// arabic
              ],
              localizationsDelegates: [
                Localization.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate
              ],
              localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales) {
                if (locale == null) {
                  return supportedLocales.first;
                }

                for (Locale supportedLocale in supportedLocales) {
                  if (supportedLocale.languageCode == locale.languageCode ||
                      supportedLocale.countryCode == locale.countryCode) {
                    return supportedLocale;
                  }
                }

                return supportedLocales.first;
              },
              debugShowCheckedModeBanner: false,
              title: 'App',
              theme: ThemeData(
                  cupertinoOverrideTheme: CupertinoThemeData(
                    primaryColor: colorPrimary,
                  ),
                  accentColor: Colors.white,
                  appBarTheme:
                  AppBarTheme(textTheme: TextTheme(title: appBarTextStyle)),
                  primarySwatch: colorWhite,
                  dialogTheme: DialogTheme(
                    titleTextStyle: textStyleDisplay2,
                    contentTextStyle: body1TextStyle,
                  ),
                  textTheme: TextTheme(
                      title: appBarTextStyle,
                      body1: body1TextStyle,
                      body2: body2TextStyle,
                      button: buttonTextStyle,
                    ),
                  inputDecorationTheme: InputDecorationTheme(
                      border: inputFocusedBorder,
                      isDense: true,
                      alignLabelWithHint: true,
                      floatingLabelBehavior: FloatingLabelBehavior.never,
                      focusedBorder: inputFocusedBorder,
                      enabledBorder: inputEnabledBorder,
                      errorBorder: errorFocusedBorder,
                      focusedErrorBorder: errorFocusedBorder,
                      errorStyle: errorLabelStyle,
                      labelStyle: inputLabelTextStyle
                  )
              ),
              onGenerateRoute: Router().onGenerateRoute,
              navigatorKey:  navigatorKey,
              initialRoute: Routes.screenNavigatorHandler,
            )
        )
    );
  });
});
}

final connector = createPushConnector();
Future<void> configurePushConnector(HadyahBloc bloc)async{
try{
connector.configure(
  onLaunch: _onLaunch,
  onResume: _onResume,
  onMessage: _onMessage,
  onBackgroundMessage: _onMessage,
);
connector.token.addListener((){
  var token=connector.token.value;
  if(token!=null){
    bloc.setFCMToken(token);
  }
  print('Token ${connector.token.value}');
});
connector.requestNotificationPermissions();

  }catch(e){
  print(e);
 }
}

void _navigateToPage(Map<String,dynamic> notificationData)async{
try{

final platform=Theme.of(_mKey.currentContext).platform;
if(platform==TargetPlatform.android){
  if(notificationData['data']['type']==ORDER_STATUS_UPDATED){
    ExtendedNavigator.ofRouter<Router>().pushNamed(
        Routes.trackOrderPage,
        arguments: int.parse(notificationData['data']['order_id'])
    );
  }
}else if(platform==TargetPlatform.iOS){
  if(notificationData['type']==ORDER_STATUS_UPDATED){
    print('type orn ${notificationData['type']}');
    ExtendedNavigator.ofRouter<Router>().pushNamed(
        Routes.trackOrderPage,
        arguments:notificationData['order_id']
    );
  }else{
    print(' else type orn ${notificationData['type']}');
  }
}
}catch(e){
print('notification error $e');
}

}
Future<dynamic> _onLaunch(Map<String,dynamic> 
notificationData)async{
await Future.delayed(Duration(milliseconds: 1500));
_navigateToPage(notificationData);
return Future.value();
}

标签: androidiosfirebaseflutterfirebase-notifications

解决方案


推荐阅读