UWP Toast Notifications - XML works, AdaptiveText does not (Windows 10, Visual Studio 2017)

551 views Asked by At

I have my Toasts set to run in a background task. If I define the XML the "old" way for the toast, everything works fine. Here's the code for that:

var template = ToastTemplateType.ToastText02;
var xml = ToastNotificationManager.GetTemplateContent(template);
var elements = xml.GetElementsByTagName("text");
var text = xml.CreateTextNode(model.Title);
elements[0].AppendChild(text);
var toast = new ToastNotification(xml);
ToastNotificationManager.CreateToastNotifier("My App").Show(toast);

If I try to use the AdaptiveText version, it doesn't run. The Event Viewer logs indicate a successful toast message sent (as it does with the first stuff), but no toast appears. Here's the code for that (which I copy/pasted directly from the tutorial on microsoft.com):

var content = new ToastContent()
{
    Launch = "app-defined-string",

    Visual = new ToastVisual()
    {
        BindingGeneric = new ToastBindingGeneric()
        {
            Children =
            {
                new AdaptiveText()   
                { 
                    Text = "Photo Share" 
                },

                new AdaptiveText()
                {
                    Text = "Andrew sent you a picture"
                },

                new AdaptiveText()
                {
                    Text = "See it in full size!"
                },

                new AdaptiveImage()
                {
                    Source = "https://unsplash.it/360/180?image=1043"
                }
            },

            AppLogoOverride = new ToastGenericAppLogo()
            {
                Source = "https://unsplash.it/64?image=883",
                HintCrop = ToastGenericAppLogoCrop.Circle
            }
        }
    }
};

var toast = new Windows.UI.Notifications.ToastNotification(content.GetXml())
{
    ExpirationTime = DateTime.Now.AddDays(model.Expiration)
};
toast.Failed += (o, args) =>
{
    var message = args.ErrorCode;
};

Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier("My App").Show(toast);

I am have OS Build 15063--same as my target for the application. I had a Template 10 project where this was happening, so I tried making a new non-Template 10 project, but the problem persists. I've also tried using NotificationExtensions.10 library from NuGet, and alternatively Windows.UI.Notifications/Microsoft.Toolkit.Uwp.Notifications; no difference with either.

1

There are 1 answers

0
codeMonkey On BEST ANSWER

It turned out that ExpirationTime = DateTime.Now.AddDays(model.Expiration) was the problem. It was getting fed 0, and as a result the Toast expired the moment it was sent, never displaying.