在Flutter应用中显示有效载荷时黑色背景

时间:2019-05-02 10:41:42

标签: dart flutter notifications payload

我为我的应用使用flutter_local_notifications软件包。当我点击通知时,有效负载显示为黑色背景,我必须按几次返回按钮才能查看应用程序页面。每次按下按钮,背景就会变亮。有什么问题吗?

class _StatefulListTileState extends State<StatefulListTile> {
  Color _iconColor1 = Colors.white;
  Color _iconColor2 = Colors.yellow;
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  initState() {
    super.initState();
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('@mipmap/ic_launcher'); 
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new FutureBuilder(
        future: saveColors(),
        builder: (context, snapshot) {
        return new ListTile(
        title: new Text(widget.title),
        leading: new IconButton(
          icon: Icon(Icons.star, color: snapshot.data.contains('${widget.id}') ? _iconColor2: _iconColor1),
          onPressed: () {
            setState(() {
              if (snapshot.data.contains('${widget.id}')) {
                _iconColor2 = Colors.white;
                _iconColor1 = Colors.white;
                flutterLocalNotificationsPlugin.cancel(widget.id);
                snapshot.data.remove('${widget.id}');
              } else {
                _iconColor1 = Colors.yellow;
                _iconColor2 = Colors.yellow;
                _showNotification(widget.id, widget.day, widget.clock);
                snapshot.data.add('${widget.id}');
              }});},),);}));}

 Future onSelectNotification(String payload) async {
    showDialog(
      context: context,
      builder: (_) {
        return new AlertDialog(...

        );},);}

  Future _showNotification(id, day, clock) async {
    //determine hour, minute and d

    var time = new Time(hour, minute, 0);
    var androidPlatformChannelSpecifics =
      new AndroidNotificationDetails('show weekly channel id',
          'show weekly channel name', 'show weekly description');
    var iOSPlatformChannelSpecifics =
      new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
    id,
    widget.title,
    widget.team, 
    d,
    time,
    platformChannelSpecifics,
    payload: widget.title + '\n' + widget.team + '\n' + widget.channel);          
  }
}

您能给我的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

确保仅在代码中执行一次该操作

@override
initState() {
    super.initState();
    var initializationSettingsAndroid =
            new AndroidInitializationSettings('@mipmap/ic_launcher'); 
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
            initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);
}