应用程序进入后台时无法处理已完成的后台任务

Can not handle completed background tasks when application goes to background

我想在收到原始推送通知后从后台任务收到完成的事件时执行一些任务(在这种情况下,它显示 toast 通知)。但我有一个问题:

当应用程序 运行 进行调试时,它正常工作,主项目可以处理来自后台任务的已完成事件并显示 Toast 通知但是当我 运行 一个没有调试的应用程序并转到背景,它不起作用,应用程序收到原始通知后没有任何显示。

这是我的代码: 在主项目中,我注册了一个后台任务:

    private async void initBackgroundTask()
    {
        string myTaskName = "Ktask";
       var status =  await BackgroundExecutionManager.RequestAccessAsync();
        // check if task is already registered
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
            if (cur.Value.Name == myTaskName)
            {                    
                cur.Value.Unregister(true);
            }
        try
        {
            // register a new task
            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
            taskBuilder.Name = myTaskName;
            taskBuilder.TaskEntryPoint = typeof(KBackgroundStuff.KBackgroundTask).ToString();
            taskBuilder.SetTrigger(new PushNotificationTrigger());

            //taskBuilder.SetTrigger(new TimeTrigger(15, false));
            BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
            myFirstTask.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted); ;
            await (new MessageDialog("Task registered")).ShowAsync();
        }
        catch(Exception e)
        {
            Debug.WriteLine("trigger " + e.Message);
        }            
    }

从后台任务处理已完成的事件:

    private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
    {
        // TODO: Add code that deals with background task completion.
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList textElements = toastXml.GetElementsByTagName("text");
        textElements[0].AppendChild(toastXml.CreateTextNode("Notification - Yeah"));
        textElements[1].AppendChild(toastXml.CreateTextNode("I'm message from your Notification!"));
        ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));


    }

后台任务:

public sealed class KBackgroundTask : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
        string content = notification.Content;
        System.Diagnostics.Debug.WriteLine(content);            
        _deferral.Complete();
    }


}

请帮助我的主项目在应用程序 运行s 没有调试的情况下从后台任务接收完成的事件。对不起我的英语

当您的应用程序进入后台时,不应显示 toast 通知。在您的代码中,您发送 toast 通知来处理后台任务的完成事件。但是在 BackgroundTaskCompletedEventHandler delegateRemarks 中声明:

Completion events are delivered only if the task completed while the application is in the foreground. If the application is suspended and then terminated, completion status is not delivered. If the application is suspended and then resumed, it is guaranteed to receive the completion notification.

当您的应用程序进入后台时,它将被挂起,因此您不会看到 toast 通知。

When the application runs with debugging, it works normally, the main project can handle completed event from background task and show Toast notification.

这是因为在使用 Visual Studio 进行调试时,Visual Studio 会阻止 Windows 挂起附加到调试器的应用程序。这是为了允许用户在应用程序 运行.

时查看 Visual Studio 调试 UI

所以在调试时,您的应用程序实际上总是 运行 在前台。更多信息请参考How to trigger suspend, resume, and background events for Windows Store apps in Visual Studio.