2016-04-15 1 views
1

GCMから正しく通知が届きましたが、メッセージをローカル(sqlite)データベースに挿入します。GCMからプッシュ通知を受け取ってアプリが実行されていないときにデータを入力

アプリが実行されていないときに通知を受け取った場合、メッセージは挿入されませんが、アプリケーションが実行されていた場合に通知が届きます。

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) 
    { 

    } 
} 

アプリケーションが実行されていないときにsqliteデータベースにアクセスできますか?

答えて

0

あなたの共有コードにDataAccess.InsertDownload()メソッドはありますか?もしそうなら、それは私が遭遇したのと同じことです。

おそらくそれを解決する最善の方法ではなく、アプリが実際に閉じられている場合は、JSON文字列をAndroidのSharedPreferencesに保存することでした。その後、アプリケーションがMainActivityの中に再びロードされ、共有プロジェクトを読み込んだ後、SharedPreferencesを読み込んでDBに保存しようとします。

以下は、これを示すコードです。 HereSettingsImplementationへのリンクです。

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

アプリが起動すると、共有コードが読み込まれるまで待ってから、通知JSONをすべて読み込もうとします。

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

はい。アプリケーションが実行されていないときにSqliteにアクセスできます。アクティビティのOnCreateで、新しいメッセージをチェックし、それに応じて更新することができます。

関連する問題