2016-04-15 4 views
1

Ich erhalte eine Benachrichtigung korrekt von GCM, aber ich möchte die Nachricht in eine lokale (SQLITE-) Datenbank einfügen.Daten einbinden, wenn Sie eine Push-Benachrichtigung von GCM erhalten und Ihre App nicht ausgeführt wurde

Wenn ich die Benachrichtigung erhalten, wenn mein App nicht läuft, ist es nicht die Nachricht einzufügen, aber wenn meine Anwendung dann lief es tut.

void SendNotification (string message) 
{ 
    var intent = new Intent (this, typeof(MainActivity)); 
    intent.AddFlags (ActivityFlags.ClearTop); 
    var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot); 

    var notificationBuilder = new Notification.Builder (this) 
     .SetSmallIcon (Resource.Drawable.icstatbutton_click) 
     .SetContentTitle ("GCM Message") 
     .SetContentText ("U heeft een nieuwe vragenlijst.") 
     .SetAutoCancel (true) 
     .SetContentIntent (pendingIntent); 

    var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); 
    notificationManager.Notify (0, notificationBuilder.Build()); 

    try 
    { 
     DataAccess.InsertDownload (message); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

Kann ich auf sqlite-Datenbank zugreifen, wenn meine Anwendung nicht ausgeführt wird?

Antwort

0

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++; 
      } 
     } 
    } 
0

Ja. Sie können auf Sqlite zugreifen, wenn die Anwendung nicht ausgeführt wird. In OnCreate Ihrer Aktivität können Sie nach neuen Nachrichten suchen und diese entsprechend aktualisieren.

Verwandte Themen