how do I use push notification to show a notification to the user when changing any status in the Flutter app

Asked

Viewed 660 times

1

Hello! I’m new in the area and wanted to know how I make to create a method that can notify the user every time you have any change in the status of his order, because I’m creating a virtual store app and wanted the user to know when you had any changes in your order.

import 'package:flutter/material.dart';

class OrderTile extends StatelessWidget {
  final String orderId;

  OrderTile(this.orderId);

  @override
  Widget build(BuildContext context) {
    return Card(
        margin: EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0),
        child: Padding(
          padding: EdgeInsets.all(8.0),
          child: StreamBuilder<DocumentSnapshot>(
              stream: Firestore.instance
                  .collection("orders")
                  .document(orderId)
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData)
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                else {
                  int status = snapshot.data["status"];

                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        "Código do pedido: ${snapshot.data.documentID}",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 4.0,
                      ),
                      Text(_buildProductsText(snapshot.data)),

                      //descrição do produto

                      SizedBox(
                        height: 4.0,
                      ),
                      Text(
                        "Status do Pedido:",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      SizedBox(
                        height: 4.0,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          _buildCircle("1", "Preparação", status, 1),
                          Container(
                            height: 1.0,
                            width: 40.0,
                            color: Colors.grey[500],
                          ),
                          _buildCircle("2", "Transporte", status, 2),
                          Container(
                            height: 1.0,
                            width: 40.0,
                            color: Colors.grey[500],
                          ),
                          _buildCircle("3", "Entrega", status, 3),
                        ],
                      )
                    ],
                  );
                }
              }),
        ));
  }

  String _buildProductsText(DocumentSnapshot snapshot) {
    String text = "Descrição:\n";
    for (LinkedHashMap p in snapshot.data["products"]) {
      text +=
          "${p["quantity"]} x ${p["product"]["title"]} (R\$ ${p["product"]["price"].toStringAsFixed(2)})\n";
    }
    text += "Total: R\$ ${snapshot.data["totalPrice"].toStringAsFixed(2)}";
    return text;
  }

  Widget _buildCircle(
      String title, String subtitle, int status, int thisStatus) {
    Color backColor;
    Widget child;

    if (status < thisStatus) {
      backColor = Colors.grey[500];
      child = Text(
        title,
        style: TextStyle(color: Colors.white),
      );
    } else if (status == thisStatus) {
      backColor = Colors.blue;
      child = Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Text(
            title,
            style: TextStyle(color: Colors.white),
          ),
          CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
          )
        ],
      );
    } else {
      backColor = Colors.green;
      child = Icon(
        Icons.check,
        color: Colors.white,
      );
    }

    return Column(
      children: <Widget>[
        CircleAvatar(
          radius: 20.0,
          backgroundColor: backColor,
          child: child,
        ),
        Text(subtitle)
      ],
    );
  }
}

In case just wanted the user to know when had some change in firebase on delivery status. Thanks in advance to those who can help with this project.

  • Since you’re new to SOF, I’ll leave you a hint: Have you researched about push notification flutter on Goole? I recommend that you first look at how it works in some tutorial on the internet and then try to implement it in your app. After trying to implement a specific problem, ask about this problem by showing what you have already done and what you have tried. As is your question she receives negative votes because it shows that there was no research on her part before coming here.

  • I recommend you study about OneSignal for notification services. There are others but it is one of the most used in the market and has a step by step implementation in Flutter projects. Without understanding how it is structured the notifications will be complicated you evolve in its implementation.

  • The notification I wish creates and only when I make change in customer status, type in the app has "prepare" status and "transport" status. In case I have another app together that makes these changes and these changes are modified there in firebase, I just wanted to create a method that I can warn the customer that their status has changed without me having to log in to firebase or onsignal to create that message for the user. I have already researched and create a standard firebase method the problem and that it only notifies what I write there and not something automatic.

1 answer

-1

I recommend to give a study on the Onesignal: https://documentation.onesignal.com/docs/flutter-sdk-setup

About sending push notifications when changing some info in the app, you will need to have the user’s Playerid, if you use the flutter+Dart pro backend of your app just vc create the functions that will trigger the push using playerid, in this link I sent you Oce will find all the steps of how to get the player id and how to do the push mails directly to the user.

  • Thank you very much I’ll look now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.