Ist Ihre DataAccess.InsertDownload()
Methode in der gemeinsamen Code? Wenn dem so ist, bin ich das Gleiche.
Wahrscheinlich nicht der beste Weg, um es zu lösen, aber was ich getan habe, war die JSON-Zeichenfolge in Android SharedPreferences
zu speichern, wenn die App tatsächlich geschlossen ist. Dann wird die App erneut geladen, und zwar innerhalb von MainActivity
. Nach dem Laden des freigegebenen Projekts versuche ich, SharedPreferences auszulesen und sie in der Datenbank zu speichern.
Nachfolgend finden Sie einige Code dies zeigt. Here ist ein Link zu SettingsImplementation
.
public async Task SaveNotifToDb(Notification notification) {
try {
DataAccess.InsertDownload (message);
} catch(System.InvalidOperationException) { //InvalidOperationException is the exception given when your shared code is not yet loaded, meaning the app is closed, so now lets save to Preferences
System.Console.WriteLine("\nIn APP.Droid.Helpers.GcmService.SaveNotificationAsync() - InvalidOperationException, the app is probably closed. Saving to Shared Preferences\n");
string notificationJson = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
string emptyCheck = SettingsImplementation.GetValueOrDefault<string>(DroidConstants.NotificationSettingKeyPart + "0", DroidConstants.NotificationSettingDefault);
if(emptyCheck.Length > 0) {
int index = 0;
while(emptyCheck.Length > 0) {
emptyCheck = SettingsImplementation.GetValueOrDefault<string>(DroidConstants.NotificationSettingKeyPart + index.ToString(), DroidConstants.NotificationSettingDefault);
index ++;
}
SettingsImplementation.AddOrUpdateValue<string>(DroidConstants.NotificationSettingKeyPart + (index - 1).ToString(), notificationJson);
} else { SettingsImplementation.AddOrUpdateValue<string>(DroidConstants.NotificationSettingKeyPart + "0", notificationJson); }
return notification;
}
}
Jetzt, wenn die Anwendung gestartet wird, warten wir auf den gemeinsamen Code zu laden und dann versuchen, alle die Benachrichtigung JSON wieder aus zu lesen.
MainActivity.OnCreate():
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
string notificationJson = SettingsImplementation.GetValueOrDefault(DroidConstants.NotificationSettingKeyPart + "0", DroidConstants.NotificationSettingDefault); //Check to see if we have a saved notification
if(notificationJson.Length > 0) {
int index = 0;
while(notificationJson.Length > 0) { //Keep trying until no more notifications can be gatherd
notificationJson = SettingsImplementation.GetValueOrDefault(DroidConstants.NotificationSettingKeyPart + index, DroidConstants.NotificationSettingDefault);
if(notificationJson.Length > 0) {
Data.Models.RemoteNotification notification = Newtonsoft.Json.JsonConvert.DeserializeObject<Data.Models.RemoteNotification>(notificationJson);
if(notification != null) {
try {
await App.RemoteNotificationRepo.InsertAsync(notification);
} catch(System.Exception e) {
System.Console.WriteLine("\nIn APP.Droid.MainActivity.OnCreate() - Exception attempting to create new in app notification\n{0}\n", e);
}
}
SettingsImplementation.Remove(DroidConstants.NotificationSettingKeyPart + index.ToString());
index++;
}
}
}