Skip to content

Toast Notifications

LukasNespor edited this page Jul 19, 2016 · 7 revisions

NotificationsExtensions for Windows 10 fully supports adaptive and interactive toast notifications. It does not support the legacy toast templates since there's no reason to use those in Windows 10. If you need those, you could download the Windows 8 source code of NotificationsExtensions.

Quickstart: Generating toast notifications

First, add the namespace declaration for Toasts (and add a reference to NotificationsExtensions if you haven't yet)...

using NotificationsExtensions.Toasts;

Then, generate your toast content

ToastContent content = new ToastContent()
{
    Launch = "lei",

    Visual = new ToastVisual
    {
        BindingGeneric = new ToastBindingGeneric()
        {
            AppLogoOverride = new ToastGenericAppLogo
            {
                HintCrop = ToastGenericAppLogoCrop.Circle,
                Source = "http://messageme.com/lei/profile.jpg"
            },
            Children =
            {
                new AdaptiveText {Text = "New message from Lei" },
                new AdaptiveText {Text = "NotificationsExtensions is great!" }
            },
            Attribution = new ToastGenericAttributionText
            {
                Text = "Alert"
            },
        }
    },                
    Actions = new ToastActionsCustom()
    {
        Inputs =
        {
            new ToastTextBox("tbReply")
            {
                PlaceholderContent = "Type a response"
            }
        },

        Buttons =
        {
            new ToastButton("reply", "reply")
            {
                ActivationType = ToastActivationType.Background,
                ImageUri = "Assets/QuickReply.png",
                TextBoxId = "tbReply"
            }
        }
    },
            
    Audio = new ToastAudio()
    {
        Src = new Uri("ms-winsoundevent:Notification.IM")
    }
};

Finally, you can retrieve the XML from the ToastContent object and use it in a WinRT ToastNotification!

XmlDocument doc = content.GetXml();

// Generate WinRT notification
var toast = new ToastNotification(doc);

// Display toast
ToastNotificationManager.CreateToastNotifier().Show(toast);

Alternatively, if you're on a server sending push notifications, you can get the XML as a string to push down to the channel.

string xml = content.GetContent();

The generated XML looks something like...

<?xml version="1.0" encoding="utf-8"?>
<toast launch="lei">
    <visual>
        <binding template="ToastGeneric">
            <image src="http://messageme.com/lei/profile.jpg" placement="appLogoOverride" hint-crop="Circle" />
            <text>New message from Lei</text>
            <text>NotificationsExtensions is great!</text>
            <text placement="attribution">Alert</text>
        </binding>
    </visual>
    <audio src="ms-winsoundevent:Notification.IM" />
    <actions>
        <input id="tbReply" type="text" placeHolderContent="Type a response" />
        <action content="reply" arguments="reply" activationType="Background" imageUri="Assets/QuickReply.png"     hint-inputId="tbReply" />
    </actions>
</toast>
Clone this wiki locally