首页 > 解决方案 > 从原生访问flutter sqflite插件的sqlite数据库

问题描述

我正在开发一个颤振应用程序,它有几个原生部分,

在颤振中使用 sqflite 插件创建数据库文件。

initDB() async{
String databasPath = await getDatabasesPath();
String path = join(databasPath,'eshaar.db');
var Db = await openDatabase(path,version: 4,onCreate: _onCreate);
return Db;

}

我想访问此类中的数据库,用于在应用程序终止时收到通知时在特定表中添加记录。


import androidx.core.app.NotificationCompat;

import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.OSNotificationReceivedResult;

import java.math.BigInteger;

public class MyNotificationService extends NotificationExtenderService {

    @Override
    protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
        OverrideSettings overrideSettings = new OverrideSettings();
        overrideSettings.extender = new NotificationCompat.Extender() {
            @Override
            public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                if (!receivedResult.restoring) {
                    // Only set custom channel if notification is not being restored
                    // Note: this would override any channels set through the OneSignal dashboard
                    return builder.setChannelId("News");
                }
                return builder.setChannelId("News");
            }
        };

        /* Do something with notification payload */
        try{
            String title = receivedResult.payload.title;
            String body  = receivedResult.payload.body;
            String sender_id = receivedResult.payload.additionalData.getString("sender_id");
            Log.d("body: ",body.toString());

            // Writes additionalData values to local database
//            SQLiteDatabase db = dbHelper.getWritableDatabase();
//            ContentValues values = new ContentValues();
//            values.put(FeedEntry.COLUMN_NAME_ARTICLE_TITLE, additionalData.articleTitle);
//            values.put(FeedEntry.COLUMN_NAME_ARTICLE_ID, additionalData.articleId);
//            long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
        }catch (Exception e){
            Log.d("Notification: ",e.toString());
        }


        return false;
    }
} ```

标签: sqliteflutterandroid-sqliteflutter-dependenciessqliteopenhelper

解决方案


推荐阅读