我有一個接受推送通知的應用程序。我必須將推送通知寫入數(shù)據(jù)庫。問題在于,在數(shù)據(jù)庫中,它僅寫入最后一條消息(在我重新進入應用程序之后),并且何時將應用程序置于后臺,何時將其置于前臺。當有新數(shù)據(jù)到達時,如何做才能使數(shù)據(jù)庫保持最新狀態(tài)?我接受推送,然后將其數(shù)據(jù)寫入變量 [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; }
1 回答

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
從您的代碼看來,您似乎在onCreate中創(chuàng)建數(shù)據(jù)庫實例,并在相同的onCreate方法中輸入數(shù)據(jù)。我看不到您的數(shù)據(jù)庫的實現(xiàn)方式或類型,但是我建議您在OnMessageReceived處理程序的Db中輸入通知,每次收到新通知時都會調用它,而不是在onCreate中,因為由于某些設備配置(例如方向更改)而很少調用onCreate。
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消