D
D
DS21072021-12-20 11:50:55
Android
DS2107, 2021-12-20 11:50:55

How to hide the service, or rather run it in the background, but so that the user does not see it in the notification shade?

I have an application similar to popular messengers (VK, Telegram), etc. I created a service that starts when the application starts or when the phone boots up, this service listens for incoming messages, calls and more. Thanks to her, I get notifications. But there is one unpleasant problem, the service icon remains in the notification blind, you can remove it only in the phone settings and the service will continue to work, as it worked. But I need to remove it programmatically.
Here is the code for my icon that is displayed when the service starts.

using Android.App;
using Android.Content;
using Android.OS;
using AndroidX.Core.App;
using Corporate_messenger.Droid.NotificationManager;

[assembly: Xamarin.Forms.Dependency(typeof(NotificationHelper))]
namespace Corporate_messenger.Droid.NotificationManager
{
    class NotificationHelper : IStaticNotification
    {
        private static string foregroundChannelId = "9003";
        private static Context context = global::Android.App.Application.Context;


        public Notification ReturnNotif()
        {
            // Building intent
            var intent = new Intent(context, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.FromBackground);
           // intent.PutExtra("Title", "Message");

            var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent);

            var notifBuilder = new NotificationCompat.Builder(context, foregroundChannelId)
                .SetContentTitle("")
                .SetContentText("")
                .SetSmallIcon(Resource.Drawable.MyChat)
                .SetOngoing(true)
                .SetContentIntent(pendingIntent);

            // Building channel if API verion is 26 or above
            if (global::Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                NotificationChannel notificationChannel = new NotificationChannel(foregroundChannelId, "Title", NotificationImportance.High);
                notificationChannel.Importance = NotificationImportance.High;
                notificationChannel.EnableLights(true);
                notificationChannel.EnableVibration(true);
                notificationChannel.SetShowBadge(true);
                notificationChannel.SetVibrationPattern(new long[] { 0L });

                var notifManager = context.GetSystemService(Context.NotificationService) as Android.App.NotificationManager;
                if (notifManager != null)
                {
                    notifBuilder.SetChannelId(foregroundChannelId);
                    notifManager.CreateNotificationChannel(notificationChannel);
                }
            }

            return notifBuilder.Build();
        }
    }
}


Here is the service start code:
using Android.App;
using Android.Content;
using Android.OS;
using Corporate_messenger.Droid.NotificationManager;
using Corporate_messenger.Models;
using Corporate_messenger.Models.Chat;
using Corporate_messenger.Service;
using Corporate_messenger.Service.Notification;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.ComponentModel;
using Xamarin.Forms;
[assembly: Xamarin.Forms.Dependency(typeof(startServiceAndroid))]
public class startServiceAndroid : IForegroundService
    {
       
        private static Context context = global::Android.App.Application.Context;

        public bool AudioCalls_Init { get; set; }

        public void StartService()
        {
             var intent = new Intent(context, typeof(NotoficationService));
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                context.StartForegroundService(intent);
            }
            else
            {
                context.StartService(intent);
            }
        }

        public void StopService()
        {
            var intent = new Intent(context, typeof(NotoficationService));
            context.StopService(intent);
        }
    }

Here is a photo
61c0447ab61f4870399957.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-12-20
@402d

the behavior you want to get rid of is built into the operating system itself.
No way.
https://developer.android.com/guide/components/for...
With no visible notification, battery optimization kills the service right away.
5 seconds and if there is no notification - dump

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question