How to use Background Service in Xamarin -Android

Vuyiswa Maseko 351 Reputation points
2021-11-30T17:59:25.73+00:00

Good Day All

I have SignalR code that runs from the App.cs file , This obviously works when the app is opened. So now i want to move the code to the background Service. So basically in my SignalR Code i show Local Notification. i have attached a simple example . i want my SignalR code to be fired and give me a notification on the foreground when the received event received something

Example Link http://www.yowfun.com/eg/ShinyExample.zip

Thanks

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-12-01T05:18:08.85+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I am not familar with SignalR. Do you want to achieve a android service that execute your SignalR code in Xamarin?

    If Yes, you need to know the Background Execution Limits in Android 8.0. SO, I advice you to use Foreground service to achieve it.

    And I notice you use Xamarin.Forms, if you want to start a service, create an Interface in the Xamarin.forms project:

       public interface IStartService  
       {  
           void StartForegroundServiceCompat();  
       }  
    

    if you execute your dependence service, you can use following code.

       DependencyService.Get<IStartService>().StartForegroundServiceCompat();  
    

    Then create a startServiceAndroid.cs in xxx.Android project to start the a service (Before android 8.0, start a service After Android 8.0 or later, you need to open a foreground service). Please see the //do you work, you can edd the new BackgroundStartup();. When the android background service is running, your BackgroundStartup could run it.

       [assembly: Dependency(typeof(startServiceAndroid))]  
       namespace ShinyExample.Droid  
       {  
           [Service]  
           class startServiceAndroid: Service, IStartService  
           {  
               public void StartForegroundServiceCompat()  
               {  
                   var intent = new Intent(Android.App.Application.Context, typeof(startServiceAndroid));  
         
         
                   if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)  
                   {  
                       Android.App.Application.Context.StartForegroundService(intent);  
                   }  
                   else  
                   {  
                       Android.App.Application.Context.StartService(intent);  
                   }  
         
               }  
         
               public override IBinder OnBind(Intent intent)  
               {  
                   return null;  
               }  
               public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;  
               public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)  
               {  
                   // From shared code or in your PCL  
         
                   CreateNotificationChannel();  
                   string messageBody = "service starting";  
         
                   var notification = new Notification.Builder(this, "10111")  
                   .SetContentTitle("Foreground")  
                   .SetContentText(messageBody)  
                   .SetSmallIcon(Resource.Drawable.icon1)  
                   .SetOngoing(true)  
                   .Build();  
                   StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);  
         
         
         
                   //do you work  
                   new BackgroundStartup();  
         
         
                     
                   return StartCommandResult.Sticky;  
               }  
         
         
               void CreateNotificationChannel()  
               {  
                   if (Build.VERSION.SdkInt < BuildVersionCodes.O)  
                   {  
                       // Notification channels are new in API 26 (and not a part of the  
                       // support library). There is no need to create a notification  
                       // channel on older versions of Android.  
                       return;  
                   }  
         
                   var channelName = Resources.GetString(Resource.String.channelName);  
                   var channelDescription = GetString(Resource.String.channel_description);  
                   var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)  
                   {  
                       Description = channelDescription  
                   };  
         
                   var notificationManager = (NotificationManager)GetSystemService(NotificationService);  
                   notificationManager.CreateNotificationChannel(channel);  
               }  
         
           }  
           
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.