Xamarin.forms xamarin android因alarmmanager通知和foregound服务而崩溃

Xamarin.forms xamarin android因alarmmanager通知和foregound服务而崩溃,xamarin.forms,xamarin.android,alarmmanager,foreground-service,Xamarin.forms,Xamarin.android,Alarmmanager,Foreground Service,我在playstore上有一个xamarin.forms android应用程序。在其中,我实现了前台服务来触发AlarmManager通知(对于android版本>=Q)。我可以看到我的应用程序在platstore上登录了一些崩溃,我认为这与这些前台服务和AlarmManager通知有关。在这里,我发布了playstore的崩溃日志,这是我所有的 java.lang.IllegalStateException crc640e87e93c5dbd1629.AlarmReceiver.n_onR

我在playstore上有一个xamarin.forms android应用程序。在其中,我实现了前台服务来触发AlarmManager通知(对于android版本>=Q)。我可以看到我的应用程序在platstore上登录了一些崩溃,我认为这与这些前台服务和AlarmManager通知有关。在这里,我发布了playstore的崩溃日志,这是我所有的

java.lang.IllegalStateException 
crc640e87e93c5dbd1629.AlarmReceiver.n_onReceive
java.lang.RuntimeException: 
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4125)
at android.app.ActivityThread.access$1400 (ActivityThread.java:270)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2062)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:237)
at android.app.ActivityThread.main (ActivityThread.java:7948)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1075)
Caused by: java.lang.IllegalStateException: 
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1687)
at android.app.ContextImpl.startService (ContextImpl.java:1632)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at android.content.ContextWrapper.startService (ContextWrapper.java:683)
at crc640e87e93c5dbd1629.AlarmReceiver.n_onReceive (Native Method)
at crc640e87e93c5dbd1629.AlarmReceiver.onReceive (AlarmReceiver.java:29)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4116)
这是另一个日志

java.lang.ClassNotFoundException
crc643622be0927bcf195.NotificationService.n_onStartCommand
java.lang.RuntimeException: 
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3755)
at android.app.ActivityThread.-wrap23 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1744)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6780)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1500)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1390)
Caused by: java.lang.ClassNotFoundException: 
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass (ClassLoader.java:380)
at java.lang.ClassLoader.loadClass (ClassLoader.java:312)
at crc643622be0927bcf195.NotificationService.n_onStartCommand (Native Method)
at crc643622be0927bcf195.NotificationService.onStartCommand (NotificationService.java:31)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3738)
以下是NotificationService类:

[Service]
public class NotificationService : Service
{
    private bool isStarted; 
    private int HIDDEN_NOTIFICATION_SERVICE_ID = 1; 
    private string HIDDEN_NOTIFICATION_CHANNEL_ID = "1";
    private string HIDDEN_NOTIFICATION_NAME = "Hidden Notification Service";


    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        if (!isStarted)
        {
            CreateHiddenNotificationChannel();
            DispatchNotificationThatServiceIsRunning();
            isStarted = true;
        }
        return StartCommandResult.NotSticky;
    }

    public override void OnDestroy()
    {
        // Remove the notification from the status bar.
        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.Cancel(HIDDEN_NOTIFICATION_SERVICE_ID);

        isStarted = false;
        base.OnDestroy();
    }

    void CreateHiddenNotificationChannel()
    {
        NotificationChannel hiddentNotificationChannel = new NotificationChannel(HIDDEN_NOTIFICATION_CHANNEL_ID, HIDDEN_NOTIFICATION_NAME, NotificationImportance.Low);
        hiddentNotificationChannel.LockscreenVisibility = NotificationVisibility.Secret;
        hiddentNotificationChannel.SetSound(null, null);
        hiddentNotificationChannel.EnableVibration(false);

        NotificationManager notificationManager = (NotificationManager)this.GetSystemService(Context.NotificationService);
        notificationManager.CreateNotificationChannel(hiddentNotificationChannel);
    }

    //start a foreground notification to keep app alive 
    private void DispatchNotificationThatServiceIsRunning()
    {
        Intent intent = new Intent(this, typeof(BroadcastReceiver.CustomReceiver));
        PendingIntent pendingIntent = PendingIntent.GetBroadcast(
                this,
                1,
                intent,
                PendingIntentFlags.UpdateCurrent
        );
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, HIDDEN_NOTIFICATION_CHANNEL_ID)
        .SetDefaults((int)NotificationDefaults.All)
        .SetSmallIcon(Resource.Drawable.icon)
        .SetSound(null)
        .SetContentIntent(pendingIntent)
        .SetPriority(NotificationCompat.PriorityMin)
        .SetAutoCancel(false)
        .SetContentTitle(AppResources.MyApp)
        .SetContentText(AppResources.ForegroundServiceNotificationMsg)
        .SetStyle(new NotificationCompat.BigTextStyle().BigText(AppResources.ForegroundServiceNotificationMsg))
        .SetOngoing(true);           

        NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
        StartForeground(HIDDEN_NOTIFICATION_SERVICE_ID, builder.Build());
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }
}
当用户重新启动设备时,从MainActivity和BroadcastReceiver类启动上述服务

if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Q)
    Android.App.Application.Context.StartForegroundService(new Intent(Android.App.Application.Context, typeof(Services.NotificationService)));

如果您重新启动设备,您会再次打开应用程序,然后推送前台服务,对吗?您可以设置通知\u通道\u ID=“1”;请设置其他值以进行测试,例如10010等@LeonLu MSFT我在问题中发布的日志来自playstore,不幸的是,我无法在我的终端复制这些崩溃。据我所知,这些崩溃应该在应用程序启动之前发生,因为我在appcenter中没有发现任何上述崩溃。我会试试你的建议。好的,等你更新。