锁定屏幕(后台)通知 - UWP

Lock Screen (Background) Notification - UWP

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

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()));
}

清单如下所示:

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

但锁屏时不弹出

I checked the settings below:

  • I enabled push notification when I was declaring background task in package manifest,

  • I turned on "Show notification on lock screen" in the windows settings,

  • and I enabled my app to run in background in the windows settings.

我可以看到它触发了后台任务(因为其他功能在后台工作)。

我错过了什么??


如果有办法改为更改欢迎消息,那对我也有用。

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

感谢任何帮助:)

我为 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);
}