Push Notification in Flutter - Local Notification Part -2

 In previous we learn how to show notification in flutter.
In this post we are going to learn Notification Customization

The default notification showing like below

Now we are going to change the color of Notification and icon 

color: Colors.pink 
icon : like

Now the notification will show like this

 

autoCancel:

By using this property we can cancel the Notification on click by set value true

How to show the Progress notification 

By using the progress property we can achieve this.

In the example i am going to show the notification progress update by every 2 seconds

import 'package:flutter/material.dart';

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.pink,
      ),
      home: MyHomePage(title: 'Flutter Push Local Notification'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  new FlutterLocalNotificationsPlugin();
  @override
  void initState() {

    print("Initialize 1");
    initNotifications();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {


    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("Generate Notification",style: TextStyle(color: Colors.white,fontSize: 20),),
          onPressed: _onTap,
          color: Colors.pink,
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );


  }
  _onTap() async {
    for(var k=0;k<10;k++)
      {
        var msg="Downloading...";
        if(k==9)
          msg="Completed";
       await Future.delayed(Duration(seconds: 5),() async {
          var androidPlatformChannelSpecifics = AndroidNotificationDetails(
              '1', 'rrtutors', 'flutter snippets',
              importance: Importance.Max, priority: Priority.High,
              showProgress: true,progress: (k+1)*10,autoCancel: false,maxProgress: 100,color: Colors.pink,
              icon: 'like'
          );
          var iOSPlatformChannelSpecifics = IOSNotificationDetails();
          var platformChannelSpecifics = NotificationDetails(
              androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
          await flutterLocalNotificationsPlugin.show(0, 'rrtutors',
              msg, platformChannelSpecifics,
              payload: 'item x');

        });
      }


  }
  void initNotifications() async{

    var initializationSettingsAndroid =
    new AndroidInitializationSettings('ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings(
        onDidReceiveLocalNotification: (i, string1, string2, string3) {
          print("received notifications");
        });
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (string) {
          print("selected notification $string");
        });


  }


}

 

While run the above code the notification will update its progress.

 

How to Navigation other screen from Notification

flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (string) {
          print("selected notification $string");

          Navigator.push(context, MaterialPageRoute(builder: (context){

            return Scaffold(
              appBar: AppBar(title: Text("Notification Screen"),),

                body: Center(
                  child: Container(
                    color: Colors.pink,
                    padding: EdgeInsets.all(8),
                    child: Text("Navigation from Notifiation ",style: TextStyle(color: Colors.white,fontSize: 20),),
                  ),
                )
            );
          }));
        });
        

With the above code on tap on Notification we are Navigating the other Screen.


Subscribe For Daily Updates