Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
As notificações Toast são alertas breves e interativos que aparecem na tela para informar o usuário sobre eventos ou atualizações. No ambiente Windows, essas notificações são especialmente úteis para aplicações que precisam comunicar informações importantes sem interromper a atividade do usuário. Este artigo vai mostrar como criar notificações Toast no Windows utilizando PowerShell e outras ferramentas disponíveis no sistema operacional.
Exemplos:
O PowerShell é uma ferramenta poderosa no Windows que permite a criação de notificações Toast de maneira simples. Abaixo está um exemplo de script PowerShell que cria uma notificação Toast.
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
$template = @"
<toast>
<visual>
<binding template="ToastGeneric">
<text>Notificação de Teste</text>
<text>Esta é uma notificação de teste criada com PowerShell.</text>
</binding>
</visual>
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$notifier.Show($toast)
Para desenvolvedores que preferem usar C#, o Windows Notification Platform oferece uma maneira robusta de criar notificações Toast. Abaixo está um exemplo de código em C#.
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
class Program
{
static void Main(string[] args)
{
string toastXmlString =
"<toast>" +
"<visual>" +
"<binding template='ToastGeneric'>" +
"<text>Notificação de Teste</text>" +
"<text>Esta é uma notificação de teste criada com C#.</text>" +
"</binding>" +
"</visual>" +
"</toast>";
XmlDocument toastXml = new XmlDocument();
toastXml.LoadXml(toastXmlString);
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("AppID").Show(toast);
}
}
Embora o CMD não suporte diretamente a criação de notificações Toast, é possível chamar scripts PowerShell a partir do CMD para alcançar esse objetivo.
powershell -command "& { [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null; $template = @\"<toast><visual><binding template='ToastGeneric'><text>Notificação de Teste</text><text>Esta é uma notificação de teste criada via CMD.</text></binding></visual></toast>\"@; $xml = New-Object Windows.Data.Xml.Dom.XmlDocument; $xml.LoadXml($template); $toast = [Windows.UI.Notifications.ToastNotification]::new($xml); $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('CMD'); $notifier.Show($toast) }"