Android 13前台服务(Foreground Service)无法推送通知且应用后台恢复重启问题求助
Android 13前台服务(Foreground Service)无法推送通知且应用后台恢复重启问题求助
各位开发大佬,我最近在做Xamarin的Android应用时遇到了两个头疼的问题,想请大家帮忙看看:
前台服务通知问题:我写了一个前台服务,在Android 12上能正常弹出通知,但升级到Android 13后,服务明明能启动(控制台能看到循环打印的日志),但就是没有任何通知显示出来。我翻了好多资料,还是没找到哪里出问题了。
应用后台恢复重启问题:我的应用从后台切换回前台时会直接重启,我本来想通过前台服务让应用在后台保持存活,这样切回来就不用重启了,但现在连通知都发不出来,也不确定如果通知正常了,这个重启问题能不能解决。
下面是我的相关代码和配置,麻烦各位帮忙排查下:
启动服务的调用方法
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); } }
前台服务相关实现
接口定义
public interface IForegroundService { void StartForegroundService(); void StopForegroundService(); bool IsForegroundServiceRunning(); }
AndroidManifest配置
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
服务类实现
[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; } } }
开机启动广播接收器
[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); } } }
备注:内容来源于stack exchange,提问作者Mert Altiparmak
相关产品推荐
相关产品推荐

