UWP背景原始推送通知

时间:2017-02-07 23:21:10

标签: c# uwp

我正在尝试将原始推送通知转到后台任务。当应用程序位于前台时,这已经有效,所以我知道收到通知并不是什么。

这是我使用的步骤。

在清单中:

  <Extensions>
    <Extension Category="windows.backgroundTasks" 
               EntryPoint="TestApp.BackgroundNotifications">
      <BackgroundTasks>
        <Task Type="pushNotification" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

背景课程:

namespace TestApp {
  public sealed class BackgroundNotifications : IBackgroundTask {
    public void Run(IBackgroundTaskInstance taskInstance) {
      Debug.WriteLine("Got background notification");
    }
  }
}

然后注册:

  BackgroundAccessStatus status =
    await BackgroundExecutionManager.RequestAccessAsync();
  BackgroundTaskBuilder taskBuilder =
    new BackgroundTaskBuilder {
      Name = "MyBackgroundNotifications",
      TaskEntryPoint = "TestApp.BackgroundNotifications"
    };
  taskBuilder.SetTrigger(new PushNotificationTrigger());
  BackgroundTaskRegistration myTask = taskBuilder.Register();

当我发送原始推送通知时,我在日志中看到了这一点,但没有看到我的调试消息:

The program '[16660] backgroundTaskHost.exe' has exited with code 1 (0x1).

我缺少一步吗?

1 个答案:

答案 0 :(得分:0)

您发布的代码是正确的。在您的步骤中,您似乎已经忘记从通用Windows平台(UWP)应用项目中引用后台任务项目。

在您的UWP应用中,您可以右键点击参考,然后选择添加参考。在项目下,选择解决方案,然后选择后台任务项目的名称,然后点击确定

在此之后,您可以重建项目,然后您的后台任务应该能够正常工作。

有关详细信息,请参阅Create and register an out-of-process background task

相关问题