I was making a code to receive notification and suddenly the application does not run anymore. Compiles, Deploys, but it takes time to consume the service and this message appears: Android project stopped continue / ok. My MainActivity:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gms.Common;
using Firebase.Messaging;
using Firebase.Iid;
using Android.Util;
using PushNotification.Plugin;
using Newtonsoft.Json.Linq;
using PushNotification.Plugin.Abstractions;
using Android.Content;
using Java.Lang;
namespace Autorizador.Droid
{
[Activity(Label = "Autorizador", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : //global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
public static Context AppContext;
protected override void OnCreate(Bundle bundle)
{
//TabLayoutResource = Resource.Layout.Tabbar;
//ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
//AppContext = this.ApplicationContext;
//CrossPushNotification.Initialize<CrossPushNotificationListener>("840100012845");
//StartPushService();
LoadApplication(new App());
}
public static void StartPushService()
{
AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
public static void StopPushService()
{
AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
{
PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
alarm.Cancel(pintent);
}
}
}
}
and my App.xaml.cs
using PushNotification.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace Autorizador
{
public partial class App : Application
{
public static CancellationTokenSource CancellationToken { get; set; }
private Label _label = new Label();
public App()
{
InitializeComponent();
//MainPage = new Autorizador.MainPage();
MainPage = new NavigationPage(new Autorizador.MainPage());
}
protected override void OnStart()
{
//CrossPushNotification.Current.Unregister();
//CrossPushNotification.Current.Register();
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
I put the using, because it could be that something should not be. What I did and did this all week, comment and uncomment what's on MainActivity in an attempt to get notifications.
EDIT1
I put a break at several points, but when it arrives at InitializeComponent () , the system stops. I give F10 and nothing, the cursor stays in the same place. Below the MainPage class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Autorizador.Model;
using Autorizador.Service;
namespace Autorizador
{
public partial class MainPage : ContentPage
{
DataService dataService;
List<Liberacao> lib;
int _idorcamento = 0;
public MainPage()
{
InitializeComponent();
dataService = new DataService();
AtualizaDados();
//Content = new ScrollView { Content = listaLibera, Orientation = ScrollOrientation.Both };
}
protected override void OnAppearing()
{
base.OnAppearing();
LimpaGeral();
AtualizaDados();
}
private async void AtualizaDados()
{
lib = await dataService.GetLiberaAsync();
listaLibera.ItemsSource = lib.OrderBy(item => item.Cliente).ToList();
}
private void listaLibera_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
try
{
var libera = e.SelectedItem as Liberacao;
_idorcamento = libera.IdOrcamento;
lblTipoVenda.Text = "Tipo de Venda: " + libera.TipoVenda;
lblVencimento.Text = "Vencimento: " + libera.Vencimento;
lblJuros.Text = "Juros: " + libera.Juros.ToString();
lblEntrada.Text = "Entrada: " + libera.Entrada;
lblAcrescimo.Text = "Acréscimo: " + libera.Acrescimo;
lblDesconto.Text = "Desconto: " + libera.Desconto;
btnItens.IsEnabled = true;
}
catch(Exception ex)
{
btnItens.IsEnabled = false;
throw new Exception(ex.Message);
}
}
private async void btnItens_Clicked(object sender, EventArgs e)
{
await Navigation.PushModalAsync(new MainPageItens(_idorcamento, lib));
}
private void LimpaGeral()
{
lblTipoVenda.Text = "Tipo de Venda:";
lblVencimento.Text = "Vencimento:";
lblJuros.Text = "Juros:";
lblEntrada.Text = "Entrada:";
lblAcrescimo.Text = "Acréscimo:";
lblDesconto.Text = "Desconto:";
btnItens.IsEnabled = false;
}
}
}