0
votes

Hi pals it's possible to use broadcastreceiver in xamarin forms

in Android Studio(Java) I use broadcastreceiver:

  1. Receiving a Firebase notification in foreground

  2. And change update of label each time recieving a notification.

its possible to use in XF ? or i should do it for Android and IOS separately

I Already have that view with c# code

i put this gif https://media.giphy.com/media/1mikDug30CysbMCGqA/giphy.gif

I want each time i receive a notificacion update the label with the new notification data:

txtUsuario.Text 

and do other functions in while the app is open

CrossFirebasePushNotification.Current.OnNotificationReceived += async (s, p) =>
        {
            //EJECUTAR ESTE METODO [OnNotificationReceived] POR DEFECTO Y NO CUANDO HAGA CLICK EN EL EVENTO
            try
            {
                Console.Out.WriteLine("TOKEN CONSOLE : + p." + p.Data);
                notificationReceived = p.Data.ToString();
                object objetoRecivido = p.Data;
                var data = new
                {
                    codigo = 0,
                    nombreUsuario = ""
                };
                //https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/FirebaseNotifications.md
                var json = JsonConvert.SerializeObject(p.Data, Newtonsoft.Json.Formatting.Indented);
                myobject = new AOCAdvancedSettings();
                myobject = JsonConvert.DeserializeObject<AOCAdvancedSettings>(json);
                System.Diagnostics.Debug.WriteLine("Received");
                txtUsuario.Text = myobject.nombreUsuario;
                Device.BeginInvokeOnMainThread(() => {

                    txtUsuario.Text = (myobject != null) ? myobject.nombreUsuario : "usuario";

                });
1

1 Answers

1
votes

It sounds like you need to invoke on the GUI thread from your event. This should work in iOS and Android in your c# code

Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
     // code to execute
});