如何处理通知混乱

时间:2019-04-24 06:28:08

标签: dart flutter

我正在使用Firebase向我的应用程序发送通知,结果是我在终端中保留了通知,但未在我的应用程序中显示 所以我该如何在小部件中处理通知,使其显示在应用程序内部

我正在尝试设置文本消息变量的状态,以使其值成为通知正文,而不是null,但它不起作用 我的代码是

[import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:idb/pages/aboutus.dart';
import 'package:idb/pages/adminpage.dart';
import 'package:idb/pages/changePassword.dart';
import 'package:idb/pages/homepage.dart';
import 'package:idb/pages/logout.dart';
import 'package:idb/pages/newsPage.dart';
import 'dart:io';

class NotificationsPage extends StatefulWidget {
  _NotificationsPageState createState() => _NotificationsPageState();
}

class _NotificationsPageState extends State<NotificationsPage> {
  final FirebaseMessaging _messaging = new FirebaseMessaging();
//String testMessage;
  Map<String, dynamic> testMessage;

  void firebaseCloudMessaging_Listeners() {
    if (Platform.isIOS) iOS_Permission();
    _messaging.getToken().then((token) {
      print('notification token $token');
    });

    _messaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        //  print('message ${message}');

        setState(() {
          testMessage = message\['notification'\]\['body'\];
          print('testMessage ${testMessage}');
        });
      },
      onResume: (Map<String, dynamic> message) async {
        //              print('message ${message}');

        //print('on resume ${message\['notification'\]\['body'\]}');

        setState(() {
          String testMessage = message\['notification'\]\['body'\];
          print('testMessage onResume ${testMessage}');
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
        //                 print('message ${message}');

        // print('on launch $message');

        setState(() {
          String testMessage = message\['notification'\]\['body'\];
          print('testMessage onLaunch ${testMessage}');
        });
      },
    );
  }

  void iOS_Permission() {
    _messaging.requestNotificationPermissions(
        IosNotificationSettings(sound: true, badge: true, alert: true));
    _messaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
  }

  int _cIndex = 1;
  void _incrementTab(index) {
    setState(() {
      _cIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: new Drawer(
        child: new ListView(children: <Widget>\[
          new Container(
            child: new DrawerHeader(
              child: Image.asset('assets/t.jpg'),
            ),
          ),
          ListTile(
            leading: Icon(Icons.home),
            title: Text('Home'),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => AdminPage()),
              );
            },
          ),
          ListTile(
            leading: Icon(Icons.credit_card),
            title: Text('My Cards'),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => HomePage()),
              );
            },
          ),
          ListTile(
            leading: Icon(Icons.message),
            title: Text('News'),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => NewsPage()),
              );
            },
          ),
          ListTile(
            leading: Icon(Icons.info),
            title: Text('About Us'),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(builder: (BuildContext context) => AboutUs()),
              );
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.lock_outline),
            title: Text('Change Password'),
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) => ChangePassword()),
              );
            },
          ),
          Logout(),
        \]),
      ),
      appBar: AppBar(
        title: Text(
          'Notifications',
          style: TextStyle(color: Colors.blueGrey),
        ),
        backgroundColor: Colors.grey\[100\],
        centerTitle: true,
        elevation: 0.0,
      ),
      body: Center(
        child: Container(
          child: Text(
            '$testMessage',
            style:
                new TextStyle(color: Colors.grey, fontWeight: FontWeight.w400),
          ),
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: Colors.orange,
        currentIndex: _cIndex,
        //fixedColor: Colors.grey\[100\],

        type: BottomNavigationBarType.shifting,
        items: \[
          BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
              color: Colors.blueGrey,
            ),
            title: Text('Home',
                style: TextStyle(
                  color: Colors.blueGrey,
                )),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.notifications,
              color: Colors.blueGrey,
            ),
            title: Text('Notifications',
                style: TextStyle(
                  color: Colors.blueGrey,
                )),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.message,
              color: Colors.blueGrey,
            ),
            title: Text('News',
                style: TextStyle(
                  color: Colors.blueGrey,
                )),
          ),
        \],
        onTap: (index) {
          if (index == 0) {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(builder: (BuildContext context) => AdminPage()),
            );
          }

          if (index == 1) {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                  builder: (BuildContext context) => NotificationsPage()),
            );
          }

          if (index == 2) {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(builder: (BuildContext context) => NewsPage()),
            );
          }
          _incrementTab(index);

          // _incrementTab(index);
        },
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    firebaseCloudMessaging_Listeners();
  }
}

] 1] 1

0 个答案:

没有答案