Hi there,
We have tried to add https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast.
Our Notification.cs :
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications; // Notifications library
using Microsoft.QueryStringDotNET; // QueryString.NET
namespace LaraveX
{
// https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast
class Notification
{
static ToastVisual visual { get; set; }
static ToastActionsCustom actions { get; set; }
static ToastContent toastContent { get; set; }
static int conversationId { get; set; }
static string image { get; set; }
static string logo { get; set; }
public static void send(string title, string content, string icon = null)
{
LoadVisual(title, content, icon); LoadActions(); LoadContent();
// And create the toast notification
var toast = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
static void LoadVisual(string title, string content, string icon = null)
{
// In a real app, these would be initialized with actual data
image = "http://www.laravex.com/assets/images/72ppi/logo.png";
logo = Sys.DIR + @"/logo.png";
// Construct the visuals of the toast
visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = title
},
new AdaptiveText()
{
Text = content
},
new AdaptiveImage()
{
Source = image
}
},
AppLogoOverride = new ToastGenericAppLogo()
{
Source = logo,
HintCrop = ToastGenericAppLogoCrop.Circle
}
}
};
}
static void LoadActions()
{
// In a real app, these would be initialized with actual data
conversationId = 384928;
// Construct the actions for the toast (inputs and buttons)
ToastActionsCustom actions = new ToastActionsCustom()
{
Inputs =
{
new ToastTextBox("tbReply")
{
PlaceholderContent = "Type a response"
}
},
Buttons =
{
new ToastButton("Reply", new QueryString()
{
{ "action", "reply" },
{ "conversationId", conversationId.ToString() }
}.ToString())
{
ActivationType = ToastActivationType.Background,
ImageUri = Sys.DIR + @"/logo.png",
// Reference the text box's ID in order to
// place this button next to the text box
TextBoxId = "tbReply"
},
new ToastButton("Like", new QueryString()
{
{ "action", "like" },
{ "conversationId", conversationId.ToString() }
}.ToString())
{
ActivationType = ToastActivationType.Background
},
new ToastButton("View", new QueryString()
{
{ "action", "viewImage" },
{ "imageUrl", image }
}.ToString())
}
};
}
static void LoadContent()
{
// Now we can construct the final toast content
ToastContent toastContent = new ToastContent()
{
Visual = visual,
Actions = actions,
// Arguments when the user taps body of toast
Launch = new QueryString()
{
{ "action", "viewConversation" },
{ "conversationId", conversationId.ToString() }
}.ToString()
};
// And create the toast notification
var toast = new ToastNotification(toastContent.GetXml());
toast.ExpirationTime = System.DateTime.Now.AddDays(2);
toast.Tag = "18365";
toast.Group = "wallPosts";
}
}
}
The system not work and return System.NullReferenceException: 'Object reference not set to an instance of an object.'
We want send local notify to windows user, u can help ? 馃檶
Your LoadContent method is setting the value of a local variable named toastContent. Not the value of the Static varible. Deleting the Type in order to set the Static variable should resolve the issue.
@StephenLPeters Today we have tryed
but the error remain. We have see on the official guide but we cant resolve with this method for send notification
Complete Class :
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications; // Notifications library
using Microsoft.QueryStringDotNET; // QueryString.NET
namespace LaraveX
{
// https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast
class Notification
{
static ToastVisual visual { get; set; }
static ToastActionsCustom actions { get; set; }
static ToastContent toastContent { get; set; }
static ToastNotification toast { get; set; }
static int conversationId { get; set; }
static string image { get; set; }
static string logo { get; set; }
public static void send(string title, string content, string icon = null)
{
LoadVisual(title, content, icon); LoadActions(); LoadContent();
toast = new ToastNotification(toastContent.GetXml());
toast.ExpirationTime = System.DateTime.Now.AddDays(2);
toast.Tag = "18365";
toast.Group = "wallPosts";
// And create the toast notification
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
static void LoadVisual(string title, string content, string icon = null)
{
// In a real app, these would be initialized with actual data
image = "http://www.laravex.com/assets/images/72ppi/logo.png";
logo = Sys.DIR + @"/logo.png";
// Construct the visuals of the toast
visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = title
},
new AdaptiveText()
{
Text = content
},
new AdaptiveImage()
{
Source = image
}
},
AppLogoOverride = new ToastGenericAppLogo()
{
Source = logo,
HintCrop = ToastGenericAppLogoCrop.Circle
}
}
};
}
static void LoadActions()
{
// In a real app, these would be initialized with actual data
conversationId = 384928;
// Construct the actions for the toast (inputs and buttons)
actions = new ToastActionsCustom()
{
Inputs =
{
new ToastTextBox("tbReply")
{
PlaceholderContent = "Type a response"
}
},
Buttons =
{
new ToastButton("Reply", new QueryString()
{
{ "action", "reply" },
{ "conversationId", conversationId.ToString() }
}.ToString())
{
ActivationType = ToastActivationType.Background,
ImageUri = Sys.DIR + @"/logo.png",
// Reference the text box's ID in order to
// place this button next to the text box
TextBoxId = "tbReply"
},
new ToastButton("Like", new QueryString()
{
{ "action", "like" },
{ "conversationId", conversationId.ToString() }
}.ToString())
{
ActivationType = ToastActivationType.Background
},
new ToastButton("View", new QueryString()
{
{ "action", "viewImage" },
{ "imageUrl", image }
}.ToString())
}
};
}
static void LoadContent()
{
// Now we can construct the final toast content
toastContent = new ToastContent()
{
Visual = visual,
Actions = actions,
// Arguments when the user taps body of toast
Launch = new QueryString()
{
{ "action", "viewConversation" },
{ "conversationId", conversationId.ToString() }
}.ToString()
};
}
}
}
@MwSpaceLLC There is also the Microsoft Q&A platform where you can ask this question (I'm not sure if this is a question specifically for the WinUI team to look at as of now) in case you won't get timely replies here (as the team is busy with WinUI 3). Microsoft Q&A is also checked by Microsoft employees who will be more than happy to help you out if they can.
@jonwis Do you know the right place to route this issue?
@tfennel might know
Try to provide an applicationId in CreateToastNotifier(). Hopefully this solves the issue.
https://stackoverflow.com/questions/32214716/showing-a-windows-10-toast-notification
Thanks for reply,
@greg-woo
@jonwis
@StephenLPeters
@Felix-Dev
Now the error not see, but our notification not appaer.
We also tried another class for test xml content but again NOT SEE TOAST:
using System;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using System.Collections.Generic;
namespace LaraveX
{
class Notify
{
public Notify(string title, string content)
{
// In a real app, these would be initialized with actual data
string logo = Sys.DIR + @"/logo.png";
// TODO: all values need to be XML escaped
// Construct the visuals of the toast
string toastVisual =
$@"<visual>
<binding template='ToastGeneric'>
<text>{title}</text>
<text>{content}</text>
<image src='{logo}' placement='appLogoOverride' hint-crop='circle'/>
</binding>
</visual>";
// TODO: all args need to be XML escaped
string toastXmlString =
$@"<toast>
{toastVisual}
</toast>";
// Parse to XML
XmlDocument toastXml = new XmlDocument();
toastXml.LoadXml(toastXmlString);
// Generate toast
var toast = new ToastNotification(toastXml);
// And create the toast notification
ToastNotificationManager.CreateToastNotifier("LaraveX").Show(toast);
}
}
}
We tried to see notification in status, but is empry:
I would suggest looking at the following code samples to see if they work. Let me know if they run properly on your machine.
https://docs.microsoft.com/en-us/samples/microsoft/windows-classic-samples/desktop-toasts/
https://docs.microsoft.com/en-us/samples/microsoft/windows-universal-samples/notifications/
@greg-woo thanks for reply,
one of this code simple work fine. Our team must include this in our project.