My ultimate goal is to have a Windows 10 app in C#.NET that displays a notification to the user. The notification should have a title, description, at least one image, and when clicked, it should open a webpage. It should also be stored in notification manager (or whatever that place in Windows that lists notifications is called) if the user doesn't click on it. So that's the goal.
I've tried a bunch of different ways of doing this, and cannot get any of them to work properly.
My current code uses the Microsoft.Toolkit.Uwp.Notifications
NuGet package and is primarily taken from this sample:
https://learn.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/adaptive-interactive-toasts?tabs=builder-syntax
The following code works great, as far as it goes:
using System;
using System.Windows;
using Microsoft.Toolkit.Uwp.Notifications;
namespace PushClient
{
public partial class App : System.Windows.Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
new ToastContentBuilder()
.AddText("My Title")
.AddText("My Description")
.Show();
}
}
}
Here is the notification it produces...
FIRST PROBLEM
The first problem is that I cannot add a custom image, even though there are supposedly three different methods through which an image can be added. I first tried this:
new ToastContentBuilder()
.AddText("My Title")
.AddText("My Description")
.AddAppLogoOverride((new Uri("https://picsum.photos/48?image=883")), ToastGenericAppLogoCrop.Circle)
.Show();
That successfully removes the default icon at the left of the notification and adds the name of the app at the bottom (since the idea is that the logo has been gotten rid of, so the app has to be identified through some other method). However, there is no new image in place of the old. Just blank space.
I also attempted this:
new ToastContentBuilder()
.AddText("My Title")
.AddText("My Description")
.AddHeroImage(new Uri("https://picsum.photos/364/180?image=1043"))
.Show();
But that changed literally nothing from the first version as far as I could tell.
Finally I tried this:
new ToastContentBuilder()
.AddText("My Title")
.AddText("My Description")
.AddInlineImage(new Uri("https://picsum.photos/360/202?image=1043"))
.Show();
That seems to have added some blank space beneath the description, but no image.
SECOND PROBLEM
Another problem is that I cannot figure out how to add a full onclick action through this process. I would be perfectly happy with either a button that needs to be clicked, or a click action on the notification itself. But however it works, it ultimately needs to open a specified URL in the user's default browser.
OTHER ATTEMPTS
I have also played with other processes of sending notifications, like the ShowBalloonTip
process. This seems to have no option at all for a custom image, which is what I want. However, I can select an image from a specified list of images, including a "warning" icon, which I've chosen in this code, and the onclick action is simple to add:
using System;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
namespace PushClient
{
public partial class App : System.Windows.Application
{
private NotifyIcon _notifyIcon = new NotifyIcon();
private void Application_Startup(object sender, StartupEventArgs e)
{
try
{
_notifyIcon = new NotifyIcon();
_notifyIcon.Icon = PushClient.Properties.Resources.accountable2you;
_notifyIcon.Visible = true;
_notifyIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
Thread t = new Thread(() => LaunchBalloonNotification(_notifyIcon, "My Title", "My Description"));
t.Start();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
Console.WriteLine("Action clicked");
}
private static void LaunchBalloonNotification(NotifyIcon ico, String title, String msg)
{
ico.ShowBalloonTip(
10000,
title,
msg,
ToolTipIcon.Warning
);
ico.BalloonTipClicked += new EventHandler(notifyIcon_BalloonTipClicked);
}
}
}
I have also tried working with ToastNotificationManager
, but the results I get with that are identical to the results I get with Microsoft.Toolkit.Uwp.Notifications
... except that ToastNotificationManager
requires an AppID
to work, and I've had difficulty figuring out how I'm properly supposed to create such a thing for my little Visual Studio test app.
Anyway, if you can point me in the right direction to help me achieve my goal here (preferably with a minimal, reproducible example!), it would be much appreciated!