我有一个接受推送通知的应用程序。我必须将推送通知写入数据库。问题在于,在数据库中,它仅写入最后一条消息(在我重新进入应用程序之后),并且何时将应用程序置于后台,何时将其置于前台。当有新数据到达时,如何做才能使数据库保持最新状态?
我接受推送,然后将其数据写入变量
[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;
}
MMMHUHU
相关分类