I created foreground service, it needs to send a notification but it doesn't send.
It looks fine, service starting and I can see from console but it doesn't push any notification ? I can't find where I'm doing wrong or missing. My expectation from this service "not restart app when user want to resume from background". My main problem is my app restart when resume from background. If I success about sending notification from service my app does restart again when user resume from background ?
Update : I could push notification at android 12 but I can't in android 13 with this code.
public void StartForegroundService()
{
var intent = new Intent(Android.App.Application.Context, typeof(ForegroundService));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
Android.App.Application.Context.StartService(intent);
}
else if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)
{
Android.App.Application.Context.StartForegroundService(intent);
}
}
Update 2 :
Interface :
public interface IForegroundService
{
void StartForegroundService();
void StopForegroundService();
bool IsForegroundServiceRunning();
}
Android Manifest :
I add those to my manifest.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Service :
[assembly: Xamarin.Forms.Dependency(typeof(ForegroundService))]
namespace İdaServis.Droid.Services
{
[Service]
public class ForegroundService : Service, IForegroundService
{
public static bool IsForegroundServiceRunning;
public override IBinder OnBind(Intent intent)
{
throw new NotImplementedException();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Task.Run(() =>
{
while(true)
{
System.Diagnostics.Debug.WriteLine("Foreground Service Running.");
Thread.Sleep(2500);
}
});
string channelID = "ForegroundServiceChannel";
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
var notificationChannel = new NotificationChannel(channelID, channelID, NotificationImportance.Default);
notificationManager.CreateNotificationChannel(notificationChannel);
}
var not = new NotificationCompat.Builder(this, channelID)
.SetContentTitle("İda Servis App Çalışıyor")
.SetContentText("Servis Çalışıyor")
.SetSmallIcon(Resource.Mipmap.icon)
.SetPriority(1)
.SetOngoing(true)
.SetChannelId(channelID)
.SetAutoCancel(true)
.Build();
StartForeground(1001,not);
return base.OnStartCommand(intent, flags, startId);
}
public override void OnCreate()
{
base.OnCreate();
IsForegroundServiceRunning = true;
}
public override void OnDestroy()
{
base.OnDestroy();
IsForegroundServiceRunning = false;
}
public void StartForegroundService()
{
var intent = new Intent(Android.App.Application.Context, typeof(ForegroundService));
Android.App.Application.Context.StartForegroundService(intent);
}
public void StopForegroundService()
{
var intent = new Intent(Android.App.Application.Context, typeof(ForegroundService));
Android.App.Application.Context.StopService(intent);
}
bool IForegroundService.IsForegroundServiceRunning()
{
return IsForegroundServiceRunning;
}
}
}
Broadcast Receiver :
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] {Intent.ActionBootCompleted})]
public class MyBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == Intent.ActionBootCompleted)
{
var foregroundServiceIntent = new Intent(Android.App.Application.Context, typeof(ForegroundService));
Android.App.Application.Context.StartForegroundService(intent);
context.StartForegroundService(foregroundServiceIntent);
}
}
}
Well, as an answer, use the following code by Liyun:
const int requestNotification = 0;
string[] notiPermission =
{
Manifest.Permission.PostNotifications
};
if ((int)Build.VERSION.SdkInt < 33) return;
if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
{
this.RequestPermissions(notiPermission, requestNotification);
}
For xamarin forms, be sure that Android Target Framework is 13 or 13 later version.