【问题标题】:RawPushNotification when app not running Windows Phone 8.1应用程序未运行 Windows Phone 8.1 时的原始推送通知
【发布时间】:2015-01-22 09:55:19
【问题描述】:

我一直在尝试编写将原始推送通知显示为 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 中,我将“锁屏通知”设置为徽章,然后在声明中添加了带有属性的后台任务,选择了推送通知并将入口点设置为 BackgroundNotificationsTask.cs

![屏幕][2]

我做错了什么还是我遗漏了什么?

编辑:

现在,当我收到推送通知时,应用程序关闭...有人知道为什么吗?

【问题讨论】:

  • 显示你是如何注册你的任务的

标签: c# windows-phone-8.1 winrt-xaml


【解决方案1】:

您做错了几件事。

1) 将您的 BackgroundTask 放在一个单独的项目中

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

2) 注册正确的类

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

BackgroundTasks.BackgroundNotificationsTask

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

3) 调用 RequestAccessAsync()

确保在注册任何任务之前调用它:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

编辑:MSDN 上有一个很好的演练https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx

【讨论】:

  • 好的,我在注册任务时摆脱了错误正在注册 - 但我没有收到 toast 消息(当应用程序运行时我会收到它们)
  • 你是否在后台任务上设置了一个断点,看看它是否至少被触发了?
  • 我不知道如何在设备上测试它(现在我在笔记本电脑上,我无法在模拟器上测试)
  • 只需通过 USB 连接您的设备,然后在调试模式下通过 Visual Studio 在设备上运行应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
相关资源
最近更新 更多