【问题标题】:Lock Screen (Background) Notification - UWP锁定屏幕(背景)通知 - UWP
【发布时间】:2020-04-06 09:05:46
【问题描述】:

我使用this 之后的以下代码测试了 toast 通知:

public static void ShowToastNotification(string message)
{
    ToastContent content = new ToastContent()
    {
        Visual = new ToastVisual()
        {
            BindingGeneric = new ToastBindingGeneric()
            {
                Children =
                {
                    new AdaptiveText()
                    {
                        Text = message
                    }
                }
            }
        }
    };

    ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(content.GetXml()));
}

Manifest 如下所示:

<VisualElements>
    ...
    <uap:LockScreen BadgeLogo="Assets\BadgeLogo.png" Notification="badgeAndTileText"/>
    ...
</VisualElements>
...
<BackgroundTasks>
    ...
    <Task Type="pushNotification"/>
    ...
</BackgroundTasks>

但屏幕锁定时它永远不会弹出。

  • 几秒内解锁就可以看到了。

  • 如果我在 15 秒后解锁,我看不到通知,因为我将持续时间设置为短。

我检查了以下设置:

  • 当我在包清单中声明后台任务时,我启用了推送通知

  • 我在 Windows 设置中开启了“在锁定屏幕上显示通知”

  • 并且我在 Windows 设置中启用了我的应用在后台运行

我可以看到它命中了后台任务(因为后台工作中的其他功能)。

我错过了什么??


如果有办法改为更改 WELCOME MESSAGE,那也适用于我。

我正在寻找一种在登录期间发生错误时通知用户的方法。

任何帮助表示赞赏:)

【问题讨论】:

  • 你用过ToastNotifier 吗?

标签: c# uwp windows-10 windows-runtime windows-10-universal


【解决方案1】:

我为 UWP windows 应用尝试了这段代码,它完美运行,你只需要创建一个通用函数

using Windows.UI.Notifications;

public static void ShowToastNotification(string title, string stringContent)
{
    ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier();

    Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");

    toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title));
    toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent));

    Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
    Windows.Data.Xml.Dom.XmlElement audio = toastXml.CreateElement("audio");
    audio.SetAttribute("src", "ms-winsoundevent:Notification.SMS");
    ToastNotification toast = new ToastNotification(toastXml);
    toast.ExpirationTime = DateTime.Now.AddSeconds(4);
    ToastNotifier.Show(toast);
}

【讨论】:

  • 您好,感谢您的评论。我也已经尝试过了,但没有区别;屏幕解锁时,这两个代码都可以正常工作,但屏幕锁定时(登录状态)它们都不起作用
  • 好的,然后试试这个link
  • 抱歉,我不确定在哪里可以找到与上面的代码不同的地方。它们不是同一个结构吗???
  • 我认为如果您的窗口被锁定(登录),即使您想解锁它,您也需要再次输入用户名和密码。然后它会显示给您登录后通知。每个用户都有自己的个人桌面应用程序,因此如果您的窗口被锁定,操作系统不会向您显示通知。
  • 哦,好吧,我明白了。因此,无论我是否像上面所做的那样更改整个设置,它都不会给我“真正”的权限来在屏幕锁定时获取通知,对吗?太难过了:(还是谢谢你的帮助!:D
猜你喜欢
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多