2017-06-11 14 views
0

私はApplication.Current.Propertiesを使用してデータを保存することができ、[キー]どのようにアンドロイドGcmServiceからフォームのプロパティをxamarinアクセス

しかし、どのようにリモート通知が到着したときにどのように私はAndroidのからこのデータを読み取ることができますか?

アプリケーションが閉じられていて、「現在の」Xamarin Formアプリケーションがない場合、このデータをどのように読み書きできますか?

[Service] 
public class GcmService : GcmServiceBase 
{ 
    public static string RegistrationID { get; private set; } 

    public GcmService() 
     : base(PushHandlerBroadcastReceiver.SENDER_IDS) { } 

    protected override void OnRegistered(Context context, string registrationId) 
    { 
     Log.Verbose("PushHandlerBroadcastReceiver", "GCM Registered: " + registrationId); 
     RegistrationID = registrationId; 
    } 

    protected override void OnMessage(Context context, Intent intent) 
    { 

     var msg = new StringBuilder(); 

     if (intent != null && intent.Extras != null) 
     { 
      foreach (var key in intent.Extras.KeySet()) 
       msg.AppendLine(key + "=" + intent.Extras.Get(key).ToString()); 
     } 



     string textoNotificacion = intent.Extras.GetString("gcm.notification.body"); 
     var nombre = "Alumno - "; 
     if (!string.IsNullOrEmpty(textoNotificacion)) 
     { 
      //***************************************** // 
      var idEstatus = "0"; 
      var textoEstatus = "Cambio de Estatus"; 

      if (textoNotificacion.Length > 2) 
      { 
       idEstatus = textoNotificacion.Substring(0, 1); 
       textoEstatus = textoNotificacion.Substring(1); 
      } 


      try 
      { 

       var app = App.Current;     

       if (app == null) 
       { 
        Read and Save data in xamarin form properties here!!! 

       } 
       else 
       { 
        var mp = (MainPage)app.MainPage; 
        nombre = mp.getNombre() + " - "; 
        mp.actualizarEstatus(idEstatus, textoEstatus); 
       } 





      } 
      catch(Exception e) 
      { 

       createNotification("Aplication ",e.Message); 
      } 




      createNotification("Aplication ", nombre +textoEstatus); 
      return; 
     } 

     createNotification("Unknown message details", msg.ToString()); 
    } 
+0

このGcmServiceは名前のとおり、サービスであり、アプリケーションの状態に関係なくOnMessageメソッドが実行されます。このように考えると意味がありません。このサービスの動作について検討していますか? –

+0

いいえ、私の質問は非常に明確です。 Xamarinフォームのプロパティでデータにアクセスするにはどうしたらいいですか?私は現在のアプリケーションがないと言うので、obviosly thatsではない方法です。私の質問は、そのデータにアクセスする別の方法です。多分パスファイルや別のライブラリ –

答えて

0

あなたはあなたのアプリケーションが閉じているときに、代わりに、あなたがデータを保存するためにSharedPreferencesを使用することができますApplication.Current.Properties [key]からデータを読み取る傾けます。 SharePreferencesは、地図データ構造を使用してデータを保存し、key-valueペアとして保存し、XML形式でデータを保存する、簡単な設定情報を格納するためのAndroidの仕組みです。

作成された保存ファイルは、/data/data/<package name>/shares_prefsフォルダに保存されます。このため、SharedPreferencesのデータは、ユーザーがアプリケーションを閉じても永続的になります。

Hereは、Xamarin.AndroidでSharePreferencesを使用する方法に関するドキュメントです。

+0

これは、andriodに対してのみ動作しますが、IOSでは動作しません。xamarinフォームを使用すると、どのようにしてxamarinフォームのプロパティにアクセスできますか? –

+0

私はIOSに精通していない、あなたは質問をするために新しいスレッドを開くことができます。 :) –

関連する問題