Xamarin 通知 Android 崩溃

Xamarin Notification Android Crash

我得到以下异常:

Java.Lang.IllegalStateException: 指定的child已经有一个parent。您必须在 child 的 parent 拳头上调用 removeVeiw()。

当运行以下代码块。这一切都从我的表单页面开始,只需按下一个按钮,该按钮使用警报管理器生成预定的通知。问题是,如果单击通知,如果应用程序仍处于活动状态,则会抛出上述异常。

如果我在 phone 上切换应用程序,然后单击通知,它会按预期恢复应用程序而不会崩溃。

以下是此工作流的相关代码片段:

来自 xamarin 页面:

 private void BtnStartJob_Clicked(object sender, EventArgs e)
    {
        lblStatus.Text = "";
        if (btnStartJob.Text == "Start Job")
        {
            tmrToggle.StartCommand.Execute(null);
            App.Manager.StartJob(App.Manager.currentTimesheet.ProjectID);
            btnStartJob.Text = "Start Break";
        }
        else if (btnStartJob.Text == "End Break")
        {
            tmrAlert.PauseCommand.Execute(null);
            tmrToggle.StartCommand.Execute(null);
            App.Manager.EndBreak(App.Manager.currentTimesheet.TimesheetID);
            btnStartJob.Text = "Start Break";
            var notificationService = DependencyService.Get<INotificationService>();
            notificationService.CancelNotification();
        }
        else if (btnStartJob.Text == "Start Break")
        {
            tmrAlert.StartCommand.Execute(null);
            tmrToggle.PauseCommand.Execute(null);
            App.Manager.StartBreak(App.Manager.currentTimesheet.TimesheetID);
            btnStartJob.Text = "End Break";

            // schedule the notification here.
            var notificationService = DependencyService.Get<INotificationService>();
            notificationService.CreateNotification("Take Action", "Your break started 1 second ago, please take action.", TimeSpan.FromSeconds(15).Ticks);
        }
    }

通知服务

    public class NotificationService : INotificationService
{

    public void CancelNotification()
    {
        var alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));            
        var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
        alarmManager.Cancel(pending);            
    }

    public void CreateNotification(string title, string message, long durationInTicks)
    {
        var duration = TimeSpan.FromTicks(durationInTicks);

        var alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver));
        alarmIntent.PutExtra("title", title);
        alarmIntent.PutExtra("message", message);

        var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
        alarmManager.Set(AlarmType.ElapsedRealtime, duration.Milliseconds, pending);
    }
}    

报警接收器:

    [BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var message = intent.GetStringExtra("message");
        var title = intent.GetStringExtra("title");

        var resultIntent = new Intent(context, typeof(MainActivity));
        resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

        var pending = PendingIntent.GetActivity(context, 0,
            resultIntent,
            PendingIntentFlags.CancelCurrent);

        var builder =
            new Notification.Builder(context)
                .SetContentTitle(title)
                .SetContentText(message)                    
                .SetSmallIcon(Resource.Drawable.WESSUClogo)
                .SetDefaults(NotificationDefaults.All);

        builder.SetContentIntent(pending);

        var notification = builder.Build();

        var manager = NotificationManager.FromContext(context);
        manager.Notify(1337, notification);
    }
}

主要活动: global::Xamarin.Forms.Platform.Android.FormsApplicationActivity {

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());                        
    }
}

对如何解决有什么建议吗?对我来说最大的问题是 object 我需要在什么上调用 removeView()?在 LoadApplication(newApp()); 上抛出此异常;我的 MainActivity 中的行。

由于完整的堆栈跟踪非常长,请单击 here 以获取 pastebin。

我自己找到了答案。

在 AlarmReceiver 中,我需要做的就是:

resultIntent.SetFlags(ActivityFlags.Singletop);