C# 当应用程序未运行Windows Phone 8.1时发出通知

C# 当应用程序未运行Windows Phone 8.1时发出通知,c#,windows-phone-8.1,winrt-xaml,C#,Windows Phone 8.1,Winrt Xaml,我一直在尝试编写后台任务,将原始推送通知显示为toast。我在应用程序运行时收到推送通知 这是我的背景任务类: public sealed class BackgroundNotificationsTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { RawNotification notification = (RawNotification)taskI

我一直在尝试编写后台任务,将原始推送通知显示为toast。我在应用程序运行时收到推送通知

这是我的背景任务类:

public sealed class BackgroundNotificationsTask : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
        string content = notification.Content;

        Debug.WriteLine("Background raw notification obtained!");

        //SendNotification(content);
    }

    private void SendNotification(string text)
    {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

        XmlNodeList elements = toastXml.GetElementsByTagName("text");
        foreach (IXmlNode node in elements)
        {
            node.InnerText = text;
        }

        ToastNotification notification = new ToastNotification(toastXml);
        ToastNotificationManager.CreateToastNotifier().Show(notification);
    }

}
然后我在MainPage.xaml.cs中注册

    private void RegisterTasks()
    {
        BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

        var taskRegistered = false;
        var exampleTaskName = "NotificationsBackground";

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == exampleTaskName)
            {
                taskRegistered = true;
                break;
            }
        }

        if(!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = exampleTaskName;
            builder.TaskEntryPoint = "BackgroundTasks.NotificationsBackground";
            builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());

            try
            {
                BackgroundTaskRegistration task = builder.Register();

                Debug.WriteLine("Background Task registered.");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Background Task register exception: " + e.ToString());
            }
        }
    }
现在在appxmanifest中,我将“锁屏通知”设置为Badge,然后在声明中,我添加了后台任务,选择了属性推送通知,并将入口点设置为BackgroundNotificationsTask.cs

!![屏幕][2]

我是在做什么错事,还是我错过了什么

编辑:

现在当我获得推送通知时,应用程序关闭。。。有人知道为什么吗


有几件事你做错了

1)将你的背景任务放在一个单独的项目中

BackgroundTask项目应该是Windows运行时组件。还要确保后台任务位于可访问的命名空间下。不要忘记从应用程序项目中引用后台任务项目

2)注册正确的类

注册后台任务时,请始终使用完全限定的类名,而不是文件名:

BackgroundTasks.BackgroundNotificationsTask
这是您必须在包清单文件和代码中使用的入口点(假设任务类在1中解释的项目中),名称空间称为
BackgroundTasks

3)调用RequestAccessAsync()

请确保在注册任何任务之前调用此选项:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

编辑:MSDN上有一个非常好的演练

有几件事你做错了

1)将你的背景任务放在一个单独的项目中

BackgroundTask项目应该是Windows运行时组件。还要确保后台任务位于可访问的命名空间下。不要忘记从应用程序项目中引用后台任务项目

2)注册正确的类

注册后台任务时,请始终使用完全限定的类名,而不是文件名:

BackgroundTasks.BackgroundNotificationsTask
这是您必须在包清单文件和代码中使用的入口点(假设任务类在1中解释的项目中),名称空间称为
BackgroundTasks

3)调用RequestAccessAsync()

请确保在注册任何任务之前调用此选项:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

编辑:MSDN上有一个非常好的演练显示您如何注册任务显示您如何注册任务确定我在注册任务时排除了错误-但我没有收到toast消息(我在应用程序运行时收到)您是否在后台任务上设置了断点以查看它是否至少被触发?我不确定如何在设备上测试它(现在我在笔记本电脑上,无法在emulator上测试)只需通过USB连接您的设备,并在调试模式下通过Visual Studio在设备上运行应用程序。好的,我在注册任务时排除了错误-但我没有收到toast消息(我在应用程序运行时收到)您是否在后台任务上设置了断点以查看它是否至少被触发?我不确定如何在设备上测试它(现在我在笔记本电脑上,无法在模拟器上测试),只需通过USB连接您的设备,并在调试模式下通过Visual Studio在设备上运行应用程序。让我们来看看。