Skip to content

Instantly share code, notes, and snippets.

@Gr8Gatsby
Last active September 30, 2020 13:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gr8Gatsby/3d471150e5b317eb1813 to your computer and use it in GitHub Desktop.
Save Gr8Gatsby/3d471150e5b317eb1813 to your computer and use it in GitHub Desktop.
Windows.UI.Notifications JavaScript example
if(Windows !== 'undefined' &&
Windows.UI !== 'undefined' &&
Windows.UI.Notifications !== 'undefined') {
var notifications = Windows.UI.Notifications;
//Get the XML template where the notification content will be suplied
var template = notifications.ToastTemplateType.toastImageAndText01;
var toastXml = notifications.ToastNotificationManager.getTemplateContent(template);
//Supply the text to the XML content
var toastTextElements = toastXml.getElementsByTagName("text");
toastTextElements[0].appendChild(toastXml.createTextNode(message));
//Supply an image for the notification
var toastImageElements = toastXml.getElementsByTagName("image");
//Set the image this could be the background of the note, get the image from the web
toastImageElements[0].setAttribute("src", "https://raw.githubusercontent.com/seksenov/grouppost/master/images/logo.png");
toastImageElements[0].setAttribute("alt", "red graphic");
//Specify a long duration
var toastNode = toastXml.selectSingleNode("/toast");
toastNode.setAttribute("duration", "long");
//Specify the audio for the toast notification
var audio = toastXml.createElement("audio");
audio.setAttribute("src", "ms-winsoundevent:Notification.IM");
//Specify launch paramater
toastXml.selectSingleNode("/toast").setAttribute("launch", '{"type":"toast","param1":"12345","param2":"67890"}');
//Create a toast notification based on the specified XML
var toast = new notifications.ToastNotification(toastXml);
//Send the toast notification
var toastNotifier = notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.show(toast);
}
@ugreg
Copy link

ugreg commented Feb 10, 2016

I noticed there are duplicate "var toastNode = toastXml.selectSingleNode("/toast");" @ lines 17 and 20, was this intentional?

@wackjob
Copy link

wackjob commented Apr 3, 2016

microsoft

@Gr8Gatsby
Copy link
Author

You know that wasn't intentional :) you could leave out the second selector

@id0Sch
Copy link

id0Sch commented Apr 26, 2017

hi, cool gist - helped me a lot
However, I noticed that when i click on a notification my app freezes and i have to close it.
Did that happen to you as well?
Thanks!

@nguyen-tam
Copy link

Do you know how to put button in the toast?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment