首页 > 解决方案 > 颤振处理通知点击不适用于所有页面

问题描述

情况是我已经登录应用程序并显示带有底部导航的主页(有 3 个项目)

如果我将句柄通知单击代码放在 main.dart 或 bottom_navigation.dart 中,那么当我单击通知时没有任何反应,但是如果我将句柄通知单击代码放在主页中,我可以单击它并重定向。但只有当我从主页点击时才有效,如果我更改为底部导航的第二项,则没有任何反应,就像第三项一样

必须将句柄通知代码放在第二个项目页面中,以便我可以点击。如何只放一次代码并且可以点击任何页面?

这是我的 main.dart(仅用于检查会话登录和重定向)

void initState() {
    super.initState();

    Firebase.initializeApp().whenComplete(() {
      var tokenStr;
      FirebaseMessaging.instance.getToken().then((token) {
        tokenStr = token.toString();
      }
      );

      EasyLoading.show(status: 'Mohon Tunggu...');
      Timer(Duration(seconds: 1), () async {
        EasyLoading.dismiss();
        if (await getIsLogin()){
          Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (context) => MyBottomNavigation()),
                (Route<dynamic> route) => false,
          );
        }else{
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(builder: (context) => LoginPage(token: tokenStr)),
                  (Route<dynamic> route) => false,
            );
        }
      });
    });
  }

这是我的底部导航代码

class _MyBottomNavigationState extends State<MyBottomNavigation> {
  int _current=0;
  static String web_inbox = "a";
  static String web_menu_inbox = "a";
  late List<Widget> _children =[
    HomePage(),
    ProfilePage(),
    Text("ERROR"),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  void onTappedBar(int index){
    setState((){
      _current = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: _children[_current],
      bottomNavigationBar: SizedBox(
        height: 60,
        child: BottomNavigationBar(
          onTap: onTappedBar,
          currentIndex: _current,
          backgroundColor: Color(0xFF003c85),
          selectedFontSize: 12,
          unselectedFontSize: 12,
          selectedItemColor: Color(0xFFf4931f),
          unselectedItemColor: Color(0xFFc0bfbf),
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/a.png', height: 25, width: 25, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/a.png', height: 25, width: 25, fit: BoxFit.fill,),
                label: "Home"
            ),
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/a.png', height: 25, width: 25, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/a.png', height: 25, width: 25, fit: BoxFit.fill,),
                label: "Profile"
            ),
            BottomNavigationBarItem(
                icon: Image.asset('asset/images/a.png', height: 23, width: 30, fit: BoxFit.fill,),
                activeIcon: Image.asset('asset/images/a.png', height: 23, width: 30, fit: BoxFit.fill,),
                label: "Inbox"
            )
          ],
        ),
      ),
    );
  }
}

这是我处理通知的主页

late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin=FlutterLocalNotificationsPlugin();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    refreshHome();

    var androidSettings = AndroidInitializationSettings("@mipmap/launcher_icon");
    var iOSSettings = IOSInitializationSettings(
      requestSoundPermission: true,
      requestBadgePermission: false,
      requestAlertPermission: true,
    );
    var initSetttings = InitializationSettings(android: androidSettings, iOS: iOSSettings);
    flutterLocalNotificationsPlugin.initialize(initSetttings, onSelectNotification: onClickNotification);

    Firebase.initializeApp().whenComplete(() {
      // foreground message
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        dataNotification(message,"foreground");
      });

      // background message while app is running
      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        dataNotification(message,"background");
      });

      // background message while app is closed
      FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) {
        dataNotification(message,"background");
      });


    });
  }

  dataNotification(RemoteMessage? message, String status){
    RemoteNotification? notification = message!.notification;
    AndroidNotification? android = message.notification?.android;
    String link = "";
    String tag = "";
    String title = "";
    String body = "";

    link = android!.link!;
    tag = android.tag!;
    title = notification!.title!;
    body = notification.body!;

    if(status=="foreground") {
      showSimpleNotification(title, body, link, tag);
    }else {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) =>
            MyWebview(url: link, statusAppbar: true, webMenu: tag)),
      );
    }
  }

  void requestPermissions() {
    flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
      alert: true,
      badge: false,
      sound: true,
    );
  }

  void onClickNotification(String? payload){
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => MyWebview(url: payload!.split("__")[1], statusAppbar: true, webMenu: payload!.split("__")[0])),
    );
  }

  showSimpleNotification(String title, String body, String link, String tag) async {
    var androidDetails = AndroidNotificationDetails('id', 'abba',
        priority: Priority.high, importance: Importance.max, enableVibration: false);
    var iOSDetails = IOSNotificationDetails();
    var platformDetails = new NotificationDetails(android: androidDetails, iOS: iOSDetails);
    await flutterLocalNotificationsPlugin.show(0, title, body,
        platformDetails, payload: tag+"__"+link);
  }

标签: flutter

解决方案


推荐阅读