How to implement a Pushnotification

Asked

Viewed 71 times

0

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

1) When the user changes the flag in the database, then a service will take that change and send it to the message server and this triggers a Push Notification.

That’s my rule. I’m trying to make mine work and it doesn’t work. I’m uninstalling the packages and leaving only what I need. The mistake that 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 is that gives dick.

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

        }
  • In firebase appears the identifier/token of your application after you compile?

  • In firebase yes, it appears. I even copy the handle and paste in the application. I just did another project and started and did not make a mistake, right now. I think it has to do with the service. I’ll try to send a notification and see what happens.

  • And your manifest, how is it?! Configured straight up?!

  • Yeah, like: pt.empresa.autorizador.com this is the name in Manifest. Now I send a Message, it takes a while and then triggers an error: An unhandled Exception occurred, but it takes another 8 or 12 min to file this error

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.