如何在通用 windows 应用程序的后台任务中使用 PushNotificationTrigger

How to use PushNotificationTrigger in Background Task in universal windows app

我搜索了很多,但没有找到任何文档或示例可以帮助我在后台任务中使用 PushNotificationTrigger,就像我们使用 SystemTrigger 和 TimeTrigger 一样。

我想在我的应用程序关闭时收到 Toast 通知。通知即将到达 windows,但不知道如何在后台处理它。 任何在 Universal windows 应用程序中使用 PushNotifications 的人请提供一些示例或文档来处理通知。

This is I am getting in Lifecycle Events 这是我用来注册任务的代码

if(!Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name == "PushBackgroundTask"))
{
   var result = await BackgroundExecutionManager.RequestAccessAsync();
   var builder = new BackgroundTaskBuilder();
   builder.Name = "PushBackgroundTask";
   builder.TaskEntryPoint = typeof(NotificationActionBackgroundTask.NotificationActionBackgroundTask).FullName;
   builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());
   BackgroundTaskRegistration task = builder.Register();
}

谢谢

我想你在这里可能有点困惑。使用PushNotification时,服务器可以向您的设备推送4种通知:Toast, Tile, Badge and Raw Notification

前三个通知将由通知中心接收并由系统处理。 Raw Notification 是一种不涉及 UI 的推送通知,其内容可用于应用程序的后台任务。

I want to receive the Toast Notifications when my app is closed.

所以不,如果您想在后台任务中使用 PushNotificationTrigger 处理 PushNotifications,您只能处理 Row Notifications。您可以参考 PushNotificationTrigger class,此 class 表示一个对象,该对象调用应用程序上的后台工作项以响应收到原始通知。

Unlike other push notifications, raw notifications don't have a specified format. The content of the payload is entirely app-defined.

所以如果你推送一个toast通知,你不需要在你的后台任务中处理它,它会被操作中心处理。但是您可以在后台任务中处理原始通知并手动对其进行烘烤。

关于PushNotificaitonTriggerRaw notification的使用方法可以参考Raw notification overview, and for toast in the background task, you can refer to the official Notifications sample.

更新

要调试带有生命周期事件的项目,您可以看一下下面的屏幕截图:

在调试的时候,你的应用生命周期依赖于vs工具,但是你可以点击这个红圈部分来调试你的应用的生命周期。