【问题标题】:Alert with closed app or in background - Flutter关闭应用程序或在后台发出警报 - Flutter
【发布时间】:2020-10-27 00:09:47
【问题描述】:

当接收到数据库中的新数据时, 即使应用程序关闭或在后台,是否可以在移动主屏幕上发出警报?

在下面的代码中,我收到了一个呼叫列表。每当有新来电时,我想提醒用户。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: Colors.orangeAccent,
        title: Text("Chamados"),
      ),
      body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            SizedBox(
              height: 10,
            ),
            Expanded(
                child: FutureBuilder(
              future: getChamados(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return new Center(
                    child: CircularProgressIndicator(),
                  );
                } else if (snapshot.data.length == 0 || tid == null || tid == "") {
                  return new Center(
                    child: Text(
                      'Nenhum Chamado.',
                      style: TextStyle(color: Colors.grey),
                    ),
                  );
                } else {
                  return ListView.builder(
                      shrinkWrap: true,
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Card(
                            elevation: 8.0,
                            margin: new EdgeInsets.symmetric(
                                horizontal: 6.0, vertical: 5.0),
                            child: Container(
                              decoration:
                                  BoxDecoration(color: Color(0xFF00aacb)),
                              child: ListTile(
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 15.0, vertical: 1.0),
                                // leading: Container(
                                //   padding: EdgeInsets.only(right: 12.0),
                                //   decoration: new BoxDecoration(
                                //       border: new Border(
                                //           right: new BorderSide(
                                //               width: 2.0,
                                //               color: Colors.white))),
                                //   child: Icon(Icons.chat_bubble,
                                //       color: Colors.white),
                                // ),
                                title: Row(
                                  children: <Widget>[
                                    Text(
                                      'Apoio solicitado',
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    Spacer(),
                                    Text(
                                        'ID: ' +
                                            snapshot.data[index].data['tid']
                                                .toString(),
                                        style: TextStyle(color: Colors.white)),
                                  ],
                                ),
                                subtitle: Row(
                                  children: <Widget>[
                                    Text(
                                        nome.toString(),
                                        style: TextStyle(color: Colors.white)),
                                    Spacer(),
                                    Text(
                                        snapshot.data[index].data['dateTime']
                                            .toString(),
                                        style: TextStyle(color: Colors.white)),
                                  ],
                                ),
                                trailing: IconButton(
                                  icon: Icon(
                                    Icons.keyboard_arrow_right,
                                    color: Colors.white,
                                    size: 28,
                                  ),
                                  onPressed: () {
                                    String titulo = "Apoio solicitado";
                                    String conteudo =
                                    snapshot.data[index].data['sinal'].toString() == "A"
                                    ? "O botão localizado no console foi pressionado. Data e hora: "+snapshot.data[index].data['dateTime'].toString()
                                    :(snapshot.data[index].data['sinal'].toString() == "B"
                                    ? "O botão localizado no banco traseiro, lado esquerdo, foi pressionado. Data e hora: "+snapshot.data[index].data['dateTime'].toString()
                                    :(snapshot.data[index].data['sinal'].toString() == "C"
                                    ? "O botão localizado no banco traseiro, lado direito, foi pressionado. Data e hora: "+snapshot.data[index].data['dateTime'].toString()
                                    :(snapshot.data[index].data['sinal'].toString() == "D"
                                    ? "O botão localizado no porta-malas foi pressionado. Data e hora: "+snapshot.data[index].data['dateTime'].toString() : "")));
                                    alertDialog(context, titulo, conteudo);
                                  },
                                ),
                              ),
                            ));
                      });
                }
              },
            ))
          ]),
    );
  }

我尝试使用推送通知,但没有成功。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    使用推送通知失败的原因是什么?我认为如果没有后端推送通知,它可能不会工作,可能是如果应用程序在后台运行。一个简单的解决方案是使用等待数据库更改的 Google Cloud 函数,然后通过该函数发送 Google Cloud 消息(推送通知)。

    有关如何执行此操作的详细信息,请参阅本文:

    Send Firebase Cloud Messaging(FCM) or Push notification to a topic using cloud function when real-time database value changes.

    【讨论】:

    • 我试图以另一种方式使用通知。我将阅读这篇文章并进行测试。很快就会返回结果。
    • 在本文中,他向您展示了如何提交主题。有没有办法使用令牌发送?在我的数据库(firestore)中,我保存了每个用户的令牌。我可以获取令牌并将通知发送给数据库发生更改的用户吗?
    • 是的,你可以。在云功能中,您在某个位置监听更改,获取更改发生的用户 ID/用户令牌(以及来自 Firestore 的任何其他必要数据),然后发送推送通知。我不确定您之前是否使用过云功能,但它们非常有用且非常易于使用。
    • 我对这些服务知之甚少,但我会尝试做测试。
    【解决方案2】:

    当应用没有被积极使用时,移动应用无法可靠地继续侦听 Firestore 更新。此时的操作系统可能会终止连接以减少电池使用量。

    (相对)可靠地向后台应用发送通知的唯一方法是监听服务器上的更改,然后使用 FCM/APNS 向设备发送推送通知。

    参见示例:

    【讨论】:

    • 谢谢,我会阅读文章并参加测试。
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多