How to implement multiple local notification in Flutter?

5,123

Solution 1

You need to change channelID for each notification you don't want to stack.

flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(your_channelID_goes_here, 
    widget.title,
    '', 
    Day.Monday,
    time,
    platformChannelSpecifics,
    payload: widget.title);

Solution 2

Just change the channelID. You have put it as 0. change it is as 0,1,2,3 then you will get multiple notifications

flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
"change Here channelID as 0 or 1 or 2 or 3", 
widget.title,
'', 
Day.Monday,
time,
platformChannelSpecifics,
payload: widget.title);
Share:
5,123
Heyran.rs
Author by

Heyran.rs

Updated on December 10, 2022

Comments

  • Heyran.rs
    Heyran.rs over 1 year

    I use flutter_local_notifications package to set notifications. I have an expandable list and every option of a title has a star icon. When I press one of white star icons, its color changes and set a notification("_showNotification" method).

    In case that I press two or more stars, my app shows only last notification, but I want to show all of them. How can I do this?

    This is whole code:

    import 'package:flutter/material.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    
    void main() {
      runApp(new MaterialApp(home: new Home()));
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Expandable List"),
          ),
          body: new ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return new ExpandableListView(ind: index, title: broadcast[index].title);
            },
            itemCount: 2,
          ),
        );
      }
    }
    
    class ExpandableListView extends StatefulWidget {
      final String title;
      final int ind;
    
      const ExpandableListView({this.ind, this.title});
    
      @override
      _ExpandableListViewState createState() => new _ExpandableListViewState();
    }
    
    class _ExpandableListViewState extends State<ExpandableListView> {
      bool expandFlag = false;
      Color _iconColor = Colors.white;
    
      @override
      Widget build(BuildContext context) {
        return new Container(
          margin: new EdgeInsets.symmetric(vertical: 1.0),
          child: new Column(
            children: <Widget>[
              new Container(
                padding: new EdgeInsets.symmetric(horizontal: 5.0),
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    new IconButton(
                        icon: new Container(
                          height: 50.0,
                          width: 50.0,
                          decoration: new BoxDecoration(
                            color: Colors.orange,
                            shape: BoxShape.circle,
                          ),
                          child: new Center(
                            child: new Icon(
                              expandFlag ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                            ),
                          ),
                        ),
                        onPressed: () {
                          setState(() {
                            expandFlag = !expandFlag;
                          });
                        }),
                    new Text(
                      widget.title,
                    )
                  ],
                ),
              ),
              new ExpandableContainer(              
                  expanded: expandFlag,
                  expandedHeight: 60.0 * 3,              
                  child: new ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                       return StatefulListTile(
                        title: broadcast[widget.ind].contents[index],
                        second: broadcast[widget.ind].time[index],
                      );
                      },
                    itemCount: broadcast[widget.ind].contents.length,
                  ))
            ],
          ),
        );
      }
    }
    
    class ExpandableContainer extends StatelessWidget {
      final bool expanded;
      final double expandedHeight;
      final Widget child;
    
      ExpandableContainer({
        @required this.child,
        this.expandedHeight,
        this.expanded = true,
      });
    
      @override
      Widget build(BuildContext context) {
        double screenWidth = MediaQuery.of(context).size.width;
        return new AnimatedContainer(
          duration: new Duration(milliseconds: 100),
          curve: Curves.easeInOut,
          width: screenWidth,
          height: expanded ? expandedHeight : 0.0,
          child: new Container(
            child: child,
            decoration: new BoxDecoration(border: new Border.all(width: 1.0)),
          ),
        );
      }
    }
    
    class StatefulListTile extends StatefulWidget {
      const StatefulListTile({this.title, this.second});
      final String title;
      final int second;
    
      @override
      _StatefulListTileState createState() => _StatefulListTileState();
    }
    
    class _StatefulListTileState extends State<StatefulListTile> {
      Color _iconColor = Colors.white;
      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(
          decoration: new BoxDecoration(
              border: new Border.all(width: 1.0, color: Colors.grey),
              color: Colors.blue,),
          child: new ListTile(
            title: new Text(widget.title), 
            leading: new IconButton(
              icon: Icon(Icons.star, color: _iconColor),
              onPressed: () {
                setState(() {
                  if (_iconColor == Colors.white) {
                    _iconColor = Colors.yellow;
                    _showNotification(widget.second);
                  } else {
                    _iconColor = Colors.white;
                  }
                });
              },
            ),
          ),
        );
      }
      Future onSelectNotification(String payload) async {
        showDialog(
          context: context,
          builder: (_) {
            return new AlertDialog(
              title: Text("PayLoad"),
              content: Text("Payload : $payload"),
            );
          },
        );
      }
    
      Future _showNotification(second) async {  
        var time = new Time(10, 18, second);
        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(
            0,
            widget.title,
            '', 
            Day.Monday,
            time,
            platformChannelSpecifics,
            payload: widget.title);
      }
    }
    
    class Broadcast {
      final String title;
      List<String> contents;
      List<int> time = [];
    
      Broadcast(this.title, this.contents, this.time);
    }
    
    
     List<Broadcast> broadcast = [
      new Broadcast(
        'A',
        ['1', '2', '3'],
        [5, 10, 15],
      ),
      new Broadcast(
        'B',
        ['4', '5'],
        [20, 25],
      ),
    ];