我创建了前台服务,它需要发送通知,但它不发送。
它看起来很好,服务启动,我可以从控制台看到,但它不推任何通知?我找不到我做错了或错过.我从这个服务的期望“不重启应用程序时,用户想从后台恢复”.我的主要问题是我的应用程序重启时,从后台恢复.如果我成功地从服务发送通知我的应用程序是否重新启动时,用户从后台恢复?
更新:我可以在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);
}
}
字符串
更新2:
的数据
接口方式:
public interface IForegroundService
{
void StartForegroundService();
void StopForegroundService();
bool IsForegroundServiceRunning();
}
型
Android清单:
我把这些加到我的清单上。
<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);
}
}
}
型
1条答案
按热度按时间disho6za1#
那么,作为答案,使用以下代码Liyun:
字符串
对于xamarin表单,请确保Android目标框架是13或13之后的版本。