后台任务中的 Toast 通知响应 [英] Toast notification response in background task

查看:36
本文介绍了后台任务中的 Toast 通知响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它可以在后台任务中显示 Toast 通知(我使用 BackgroundTaskBuilder).在通知中,我使用了两个按钮,它们应该有两个不同的功能,但我无法得到通知的响应.

我在互联网上读到我应该为此启动另一个后台任务,但我无法在后台任务中启动另一个后台任务.

所以我的问题是:我怎样才能知道用户在通知中点击了什么按钮?

感谢您的帮助.

解决方案

在 Windows 10 中,我们可以从前台或后台处理 Toast 通知激活.在 Windows 10 中,它引入了自适应和交互式 Toast 通知,这些通知在

当用户单击查看更多详细信息"按钮时,它会将应用置于前台.Application.OnActivated 方法 将被调用使用新的激活种类ToastNotification.我们可以像下面这样处理这个激活:

protected override void OnActivated(IActivatedEventArgs e){//获取根框架Frame rootFrame = Window.Current.Content as Frame;//TODO: 像在 OnLaunched 中一样初始化根框架//处理 toast 激活if (e 是 ToastNotificationActivatedEventArgs){var toastActivationArgs = e as ToastNotificationActivatedEventArgs;//获取参数字符串 args = toastActivationArgs.Argument;//TODO: 根据参数处理激活}//TODO: 处理其他类型的激活//确保当前窗口处于活动状态Window.Current.Activate();}

当用户点击稍后提醒我"按钮时,它将触发后台任务而不是激活前台应用程序.所以不需要在后台任务中启动另一个后台任务.

为了处理来自 toast 通知的后台激活,我们需要创建并注册一个后台任务.这后台任务应在应用清单中声明为 系统事件" 任务,并将其触发器设置为 ToastNotificationActionTrigger.然后在后台任务中,使用 ToastNotificationActionTriggerDetail检索预定义的参数以确定单击哪个按钮,例如:

公共密封类 NotificationActionBackgroundTask : IBackgroundTask{公共无效运行(IBackgroundTaskInstance taskInstance){var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;如果(详细信息!= null){字符串参数 = details.Argument;//执行任务}}}

有关详细信息,请参阅自适应和交互式 Toast 通知,尤其是 处理激活(前台和后台).还有完整示例 GitHub.

I'm writing an application which can show toast notifications in a background task (I use BackgroundTaskBuilder). In the notification I'm using two button, which should do two different function, but I can't get the notification's response.

I read on the internet that I should start another background task for this, but I can't start another background task in a backround task.

So my question is: How can I get that the user what button clicked in the notification?

Thanks for the help.

解决方案

In Windows 10, we can handle a toast notification activation from either foreground or background. In Windows 10, it introduces adaptive and interactive toast notifications which have an activationType attribute in <action> element. With this attribute, we can specify what kind of activation this action will cause. Using following toast for example:

<toast launch="app-defined-string">
  <visual>
    <binding template="ToastGeneric">
      <text>Microsoft Company Store</text>
      <text>New Halo game is back in stock!</text>
    </binding>
  </visual>
  <actions>
    <action activationType="foreground" content="See more details" arguments="details"/>
    <action activationType="background" content="Remind me later" arguments="later"/>
  </actions>
</toast>

When user clicks "See more details" button, it will bring the app to foreground. Application.OnActivated method will be invoked with a new activation kindToastNotification. And we can handle this activation like following:

protected override void OnActivated(IActivatedEventArgs e)
{
    // Get the root frame
    Frame rootFrame = Window.Current.Content as Frame;

    // TODO: Initialize root frame just like in OnLaunched

    // Handle toast activation
    if (e is ToastNotificationActivatedEventArgs)
    {
        var toastActivationArgs = e as ToastNotificationActivatedEventArgs;

        // Get the argument
        string args = toastActivationArgs.Argument;
        // TODO: Handle activation according to argument
    }
    // TODO: Handle other types of activation

    // Ensure the current window is active
    Window.Current.Activate();
}

When user clicks "Remind me later" button, it will trigger a background task instead of activating the foreground app. So there is no need to start another background task in a background task.

To handle the background activation from toast notifications, we need to create and register a background task. The background task should be declared in app manifest as "System event" task and set its trigger to ToastNotificationActionTrigger. Then in the background task, use ToastNotificationActionTriggerDetail to retrieve pre-defined arguments to determine which button is clicked like:

public sealed class NotificationActionBackgroundTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;

        if (details != null)
        {
            string arguments = details.Argument;
            // Perform tasks
        }
    }
}

For more info, please see Adaptive and interactive toast notifications, especially Handling activation (foreground and background). And also the complete sample on GitHub.

这篇关于后台任务中的 Toast 通知响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆