从SQL数据库发送带有时间的通知

时间:2019-09-26 09:45:06

标签: java android

我需要一些小帮助,如标题所述,我需要将通知发送到保存在sql(Room)数据库中的特定时间。我尝试使用alarmManager和Broadcast接收器,但是我无法弄清楚如何在应用程序关闭时从数据库获取时间。

1 个答案:

答案 0 :(得分:0)

您应该启动一个后台服务,例如Periodic,您可以将其设为class MyBackgroundWorker extends Worker { public MyBackgroundWorker(Context context, WorkParameters workParameters){ super(context, workParameters); } @Override Result doWork(){ //make room request here //after selection is successful schedule your alarm manager notification here } } (每15分钟最少运动一次重复一次)。即使您的手机重启,它也会运行。 WorkManager非常易于配置和使用。

示例:

WorkManager.getInstance(myContext).enqueue(OneTimeWorkRequestBuilder<UploadWorker>()
        .build());

//or you can choose periodic requests.

现在,如何启动它:

在您需要的那一刻:

  import 'package:flutter/material.dart';

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
        title: 'Flutter Demo',
        home: HomePage(),
        );
    }
    }

    class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() => _HomePageState();
    }

    class _HomePageState extends State<HomePage> {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
        appBar: AppBar(),
        body: Center(
            child: Padding(
            padding: EdgeInsets.all(18.0),
            child: Container(
            padding: EdgeInsets.only(
                top: 18.0,
            ),
            margin: EdgeInsets.only(top: 13.0, right: 8.0),
            decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.circular(16.0),
                boxShadow: <BoxShadow>[
                    BoxShadow(
                    color: Colors.black26,
                    blurRadius: 0.0,
                    offset: Offset(0.0, 0.0),
                    ),
                ]),
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                SizedBox(
                    height: 20.0,
                ),
                Center(
                    child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: new Text("Your Text",
                        style: TextStyle(fontSize: 30.0, color: Colors.black)),
                )),
                SizedBox(height: 24.0),
                InkWell(
                    child: Container(
                    padding: EdgeInsets.only(top: 4.0, bottom: 4.0),
                    decoration: BoxDecoration(
                        color: Colors.green,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(16.0),
                            bottomRight: Radius.circular(16.0)),
                    ),
                    ),
                    onTap: () {
                    Navigator.pop(context);
                    },
                )
                ],
            ),
            ),
        )),
        );
    }
    }

Please refer to the documentation for more