Однако после неоднократных попыток и прочтения различных руководств я все еще не вижу уведомления. Ниже представлена услуга:
Код: Выделить всё
using Android.Content;
using Android.OS;
using AndroidX.Core.App;
namespace ProjectNecrozma.AndroidTasksHandler
{
[Service(Name = "com.projectnecrozma.android.tasks.handler.BackgroundService")]
public class BackgroundService : Service
{
private const int NOTIFICATION_ID = 1;
private const string CHANNEL_ID = "project_necrozma_channel";
public override void OnCreate()
{
base.OnCreate();
}
public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId)
{
CreateNotificationChannel();
if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
{
StartForeground(NOTIFICATION_ID, CreateNotificationService(), Android.Content.PM.ForegroundService.TypeSpecialUse);
}
else
{
StartForeground(NOTIFICATION_ID, CreateNotificationService());
}
return StartCommandResult.Sticky;
}
private void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channel = new NotificationChannel(CHANNEL_ID, "Project Necrozma Tasks Handler Channel", NotificationImportance.Default)
{
Description = "Channel for Project Necrozma Tasks Handler"
};
var notificationManager = (NotificationManager?)GetSystemService(NotificationService);
notificationManager?.CreateNotificationChannel(channel);
}
}
private Notification CreateNotificationService()
{
var notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle("Project Necrozma Tasks Handler")
.SetContentText("The service is running.")
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
.SetAutoCancel(false);
return notificationBuilder.Build();
}
public override IBinder? OnBind(Intent? intent)
{
return null;
}
}
}
Код: Выделить всё
Код: Выделить всё
using Android.Content;
using Android.OS;
namespace ProjectNecrozma.AndroidTasksHandler
{
[Activity(Label = "TasksHandler", MainLauncher = true, NoHistory = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
var intent = new Intent(this, typeof(BackgroundService));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
StartForegroundService(intent);
}
else
{
StartService(intent);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... sing-net-8