C# 如何更新数据库?

C# 如何更新数据库?,c#,firebase,xamarin,firebase-realtime-database,C#,Firebase,Xamarin,Firebase Realtime Database,我有一个接受推送通知的应用程序。我必须将推送通知写入数据库。问题是,在数据库中,它只写入最后一条消息(在我重新进入应用程序之后),以及应用程序处于后台和前台时。当新数据到达时,如何使数据库保持最新 我接受push,然后将其数据写入变量 [Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class MyFirebaseMessagingService : Fi

我有一个接受推送通知的应用程序。我必须将推送通知写入数据库。问题是,在数据库中,它只写入最后一条消息(在我重新进入应用程序之后),以及应用程序处于后台和前台时。当新数据到达时,如何使数据库保持最新

我接受push,然后将其数据写入变量

      [Service]

   [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {

        const string TAG = "MyFirebaseMsgService";

        public override void HandleIntent(Intent intent)
        {
            try
            {
                if (intent.Extras != null)
                {
                    var builder = new RemoteMessage.Builder("MyFirebaseMessagingService");

                    foreach (string key in intent.Extras.KeySet())
                    {
                        builder.AddData(key, intent.Extras.Get(key).ToString());
                    }

                    this.OnMessageReceived(builder.Build());
                }
                else
                {
                    base.HandleIntent(intent);

                }
            }
            catch (Exception)
            {
                base.HandleIntent(intent);
            }
        }

        public override void OnMessageReceived(RemoteMessage message)
        {

            Log.Debug(TAG, "From: " + message.From);
            Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
            SendNotification(message.GetNotification().Body, message.Data, message.GetNotification().Title);
           // Here I write the push - notification data into variables
            X.Instance.title = message.GetNotification().Title;
            X.Instance.body = message.GetNotification().Body;

        }

        private void OnStartCommand()
        {
            throw new NotImplementedException();
        }

        public class X
        {

            public static X Instance = new X();

            public  string title;
            public string body;
        }
在本课程中,我填写数据库:

[Service]
[Activity(Label = "Message")]
public class FormMessageActivity : Activity
{
    ListView lstData;
    List<Message> lstSource = new List<Message>();
     DataBase db;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.FormMessage);
        db = new DataBase();
        db.CreateDataBase();
        lstData = FindViewById<ListView>(Resource.Id.listView);
        Message message = new Message()
        {
            //And here I add to the database
            Some = MyFirebaseMessagingService.X.Instance.title,
            Notification = MyFirebaseMessagingService.X.Instance.body
        };
        if (message.Notification != null)
        {
            db.InsertIntoTableMessage(message);
            LoadData();
        }

        LoadData();
        lstData.ItemClick += (s, e) =>
        {
            //Set background for selected item
            for (int i = 0; i < lstData.Count; i++)
            {
                if (e.Position == i) StartActivity(typeof(MessageActivity));

            }
        };
    }
   public void LoadData()
    {
        lstSource = db.SelectTableMessage();
        var adapter = new ListViewAdapter(this, lstSource);
        lstData.Adapter = adapter;
    }
}
[服务]
[活动(Label=“Message”)]
公共类FormMessageActivity:活动
{
列表视图数据;
List lstSource=新列表();
数据库数据库;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.FormMessage);
db=新数据库();
db.CreateDataBase();
lstData=findviewbyd(Resource.Id.listView);
消息消息=新消息()
{
//这里我添加到数据库中
Some=MyFirebaseMessagingService.X.Instance.title,
通知=MyFirebaseMessagingService.X.Instance.body
};
if(message.Notification!=null)
{
db.InsertIntoTableMessage(消息);
LoadData();
}
LoadData();
lstData.ItemClick+=(s,e)=>
{
//设置所选项目的背景
for(int i=0;i
从您的代码来看,似乎您正在onCreate中创建一个DB实例,并在同一个onCreate方法中输入数据。我看不出您的db是如何实现的,或者它是什么类型的db,但我建议您在OnMessageReceived处理程序中的db中输入通知,该处理程序在每次收到新通知时都会调用,而不是在onCreate中,因为onCreate是少量调用的,并且由于某些设备配置(如方向更改)的原因