Nehal osama
Nehal osama

Reputation: 65

Flutter Push Notification to user

I am working on a client application ordering products, I want to push notification for specific user when his order accepted and when delivery arrive to him, i am new in flutter and i don't know how start.

What is the correct way to implement that and achieve good performance, I don’t know which way to start ?

Upvotes: 0

Views: 2136

Answers (2)

Nehal osama
Nehal osama

Reputation: 65

Thanks alot @GrandMagus for helping, this is the solution of my question

class _MyAppState extends State<MyApp> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  Timer timer;

  @override
  void initState() {
    super.initState();
    var initializationSettingsAndroid =
        AndroidInitializationSettings('flutter_devs');
    var initializationSettingsIOs = IOSInitializationSettings();
    var initSetttings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOs);

    flutterLocalNotificationsPlugin.initialize(initSetttings,
        onSelectNotification: onSelectNotification);
    timer=Timer.periodic(Duration(seconds: 15), (Timer t) => showNotification());
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }
  Future onSelectNotification(String payload) {
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return MyApp();
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.red,
        title: new Text('Flutter notification demo'),
      ),
    );
  }

Upvotes: 3

GrandMagus
GrandMagus

Reputation: 742

You should try reading the documentation for this package, maybe this will help you. Local Push Notifications It stores the notification you want to send to user locally on their phone and send it on a timer or when you trigger some action. You must initialize the package in the main function of your main.dart. I've tried this package with an Alarm app, it works fine.

Upvotes: 1

Related Questions