是否可以在react-native-background-job React Native中运行SQLite Storage的SQL查询

时间:2018-10-29 10:26:22

标签: react-native

我正在开发一个药丸提醒应用程序,该应用程序需要设置药丸提醒功能。 我需要做的是-

  • 执行后台进程并从SQLite Storage获取提醒。
  • 如果有提示而不是发布本地通知

我正在使用的库-

https://www.npmjs.com/package/react-native-push-notification

https://www.npmjs.com/package/react-native-background-job

https://www.npmjs.com/package/react-native-sqlite-storage

我已经实现了运行良好的后台进程

import BackgroundJob from 'react-native-background-job';
import SQLite from 'react-native-sqlite-storage';
import PushNotification from 'react-native-push-notification';

//this is the function is getting invoked in background 
const pushNoti = () => {

    var db = SQLite.openDatabase({ name: "MedCare.db", createFromLocation: 1 }, this.okCallback, this.errorCallback);

    db.transaction((tx) => {

        tx.executeSql('SELECT * FROM reminders', [], (tx, results) => {

            var len = results.rows.length;

            if(len > 0){

                PushNotification.localNotification({

                    title: "Its time to take you Pills",

                });

            }

        });

    });

}

const backgroundJob = {
    jobKey: "myPillReminders",
    job: () => pushNoti()
};

BackgroundJob.register(backgroundJob);

export defalt class Reminder extends Component {

    componentDidMount() {

        var backgroundSchedule = {
            jobKey: "myPillReminders",
            period: 5000,
            exact: true,
            allowExecutionInForeground: true
        }

        BackgroundJob.schedule(backgroundSchedule);

    }

}

0 个答案:

没有答案