How to implement a PushNotification

0

I know I need a notification server for this. I chose the firebase because it is free and does not require a credit card. What I want to do:

1) When the user changes the Flag to the bank, then a service will pick up this change and send it to the messaging server and this will trigger a Push Notification.

That's my rule. I'm trying to make mine work and it does not work. I'm uninstalling the packages and leaving only what I need. The error it gives is: Unhandled exception occured. My Activity:

[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");

            //CrossPushNotification.Current.Register();

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

Minh CrossPushNotificationListener

public class CrossPushNotificationListener : IPushNotificationListener
    {
        public CrossPushNotificationListener()
        {
        }

        public void OnError(string message, DeviceType deviceType)
        {

        }

        public void OnMessage(JObject values, DeviceType deviceType)
        {

        }

        public void OnRegistered(string token, DeviceType deviceType)
        {

        }

        public void OnUnregistered(DeviceType deviceType)
        {

        }

        public bool ShouldShowNotification()
        {
            return true;
        }
    }

When I try to register the plugin in App.xaml.cs, it sucks.

protected override void OnStart()
        {
              CrossPushNotification.Current.Unregister();
              CrossPushNotification.Current.Register();

        }
    
asked by anonymous 24.09.2017 / 21:51

0 answers