Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF native windows 10 toasts

Tags:

wpf

windows-10

Using .NET WPF and Windows 10, is there a way to push a local toast notification onto the action center using c#? I've only seen people making custom dialogs for that but there must be a way to do it through the os.

like image 384
shady Avatar asked Feb 16 '16 19:02

shady


People also ask

What is toast notifier in Windows 10?

A toast notification is a message that your app can construct and deliver to your user while they are not currently inside your app. This quickstart walks you through the steps to create, deliver, and display a Windows 10 or Windows 11 toast notification using rich content and interactive actions.

How do I turn off toast notifications in Windows 10?

Users can disable toast notifications in Settings > System > Notification & actions – simply turn off the setting Get notifications from apps and other senders. There is also a group policy setting that can disable toast notifications and lock the setting so the user can't turn it back on.

What are toasts in Windows?

Toast notifications are similar to a popup message. These notifications provide time-sensitive information about events that occur while an application is running. Toast notifications appear in the foreground whether Windows is currently in desktop mode, displaying the lock screen, or running another application.


Video Answer


2 Answers

You can use a NotifyIcon from System.Windows.Forms namespace like this:

class Test 
{
    private readonly NotifyIcon _notifyIcon;

    public Test() 
    {
        _notifyIcon = new NotifyIcon();
        // Extracts your app's icon and uses it as notify icon
        _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
        // Hides the icon when the notification is closed
        _notifyIcon.BalloonTipClosed += (s, e) => _notifyIcon.Visible = false;
    }

    public void ShowNotification() 
    {
        _notifyIcon.Visible = true;
        // Shows a notification with specified message and title
        _notifyIcon.ShowBalloonTip(3000, "Title", "Message", ToolTipIcon.Info);
    }

}

This should work since .NET Framework 1.1. Refer to this MSDN page for parameters of ShowBalloonTip.

As I found out, the first parameter of ShowBalloonTip (in my example that would be 3000 milliseconds) is generously ignored. Comments are appreciated ;)

like image 106
Franz Wimmer Avatar answered Sep 22 '22 13:09

Franz Wimmer


I know this is an old post but I thought this might help someone that stumbles on this as I did when attempting to get Toast Notifications to work on Win 10.

This seems to be good outline to follow - Send a local toast notification from desktop C# apps

I used that link along with this great blog post- Pop a Toast Notification in WPF using Win 10 APIs

to get my WPF app working on Win10. This is a much better solution vs the "old school" notify icon because you can add buttons to complete specific actions within your toasts even after the notification has entered the action center.

Note- the first link mentions "If you are using WiX" but it's really a requirement. You must create and install your Wix setup project before you Toasts will work. As the appUserModelId for your app needs to be registered first. The second link does not mention this unless you read my comments within it.

TIP- Once your app is installed you can verify the AppUserModelId by running this command on the run line shell:appsfolder . Make sure you are in the details view, next click View , Choose Details and ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.

Here's a snipit of code that I used. One thing two note here, I did not install the "Notifications library" mentioned in step 7 of the first link because I prefer to use the raw XML.

private const String APP_ID = "YourCompanyName.YourAppName";

    public static void CreateToast()
    {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
            ToastTemplateType.ToastImageAndText02);

        // Fill in the text elements
        XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
        stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
        stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));

        // Specify the absolute path to an image
        string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Your Path To File\Your Image Name.png";
        XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
        imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;

        // Change default audio if desired - ref - https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
        XmlElement audio = toastXml.CreateElement("audio");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");  
        audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
        //audio.SetAttribute("loop", "false");
        // Add the audio element
        toastXml.DocumentElement.AppendChild(audio);

        XmlElement actions = toastXml.CreateElement("actions");
        toastXml.DocumentElement.AppendChild(actions);

        // Create a simple button to display on the toast
        XmlElement action = toastXml.CreateElement("action");
        actions.AppendChild(action);
        action.SetAttribute("content", "Show details");
        action.SetAttribute("arguments", "viewdetails");

        // Create the toast 
        ToastNotification toast = new ToastNotification(toastXml);

        // Show the toast. Be sure to specify the AppUserModelId
        // on your application's shortcut!
        ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
    }
like image 33
Kevin Moore Avatar answered Sep 25 '22 13:09

Kevin Moore