0

I can store data using Application.Current.Properties [key]

But how how can I read this data from Android when a remote notification arrives ?

If the aplication is closed and there is no "current" Xamarin Form Aplication how can I read/write this data?

 [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());
    }
David H
  • 25
  • 1
  • 4
  • This GcmService, as the name suggests, is a service, and the OnMessage method will be executed regardless the application state... Thinking this way the question is making no sense... Are you considering about this service behavior? – Diego Rafael Souza Jun 11 '17 at 19:38
  • No, my question is very clear. How can I access the data in Xamarin Form Properties. I say there is no current App so obviosly thats not the way. My question is about another way yo access that data. Maybe a path file or another library – David H Jun 19 '17 at 12:37

1 Answers1

0

You cant read data from Application.Current.Properties [key] when your App is closed, instead, you can use SharedPreferences to store data. SharePreferences is a mechanism in Android for storing some simple configuration information, using the Map data structure to store data, stored as key-value pairs, and storing the data in the XML format.

The created storage file is saved under the /data/data/<package name>/shares_prefs folder. For this reason data in SharedPreferences will be persistent even though user closes the application.

Here is a document about how to use SharePreferences in Xamarin.Android.

York Shen
  • 9,014
  • 1
  • 16
  • 40