shernandezp / XamarinForms.LocationService

MAUI Background Services, Background Location Updates, Location Updates, BroadCastReceiver
https://github.com/shernandezp
Apache License 2.0
71 stars 25 forks source link

Android Runtime Exception #12

Closed johng1492 closed 2 years ago

johng1492 commented 2 years ago

Describe the bug When trying to start location service, in foreground, on Android, I get the error: Bad notification posted from package com.xxx.xxxxxxx: Couldn't create icon: StatusBarIcon(.

The issue is on starting the service and I have tried for hours on what the issue is. I think it's a NULL that I'm passing someplace that is expecting a value.

To Reproduce Steps to reproduce the behavior:

  1. Call: MessagingCenter.Send(message, "ServiceStarted"); from FORMS

  2. Here is the method (same as your sample): MessagingCenter.Subscribe(this, "ServiceStarted", message => { if (!IsServiceRunning(typeof(AndroidLocationService))) { if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { StartForegroundService(serviceIntent); } else { StartService(serviceIntent); } } });

  3. Here is the LocationService StartCommand: public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { _cts = new CancellationTokenSource();

        Notification notif = DependencyService.Get<INotification>().ReturnNotif();
        StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notif);
    
        Task.Run(() => {
            try
            {
                var locShared = new Location();
                locShared.Run(_cts.Token).Wait();
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new StopServiceMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "ServiceStopped")
                    );
                }
            }
        }, _cts.Token);
    
        return StartCommandResult.Sticky;
    }
  4. It seems like it bombs on: locShared.Run(_cts.Token).Wait(); above.

Expected behavior To start the location service and track the user's location

Smartphone (please complete the following information):

shernandezp commented 2 years ago

I understand you are getting an error based on a change you made to the original code, the current code does not throw any kind of error. I suggest you go throw the documentation of Messaging Center: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center