Skip to content

Instantly share code, notes, and snippets.

@Gr8Gatsby
Last active January 24, 2020 11:09
Show Gist options
  • Save Gr8Gatsby/11d8c5a54507d52f41dc to your computer and use it in GitHub Desktop.
Save Gr8Gatsby/11d8c5a54507d52f41dc to your computer and use it in GitHub Desktop.
Create a custom toast message and respond to the activation event recieved from the user selection.
(function () {
"use strict";
function createToast(message, imgUrl, imgAlt) {
// Namespace: Windows.UI.Notifications
if (typeof Windows !== 'undefined' &&
typeof Windows.UI !== 'undefined' &&
typeof Windows.UI.Notifications !== 'undefined') {
// Setup variables for shorthand
var notifications = Windows.UI.Notifications,
templateType = notifications.ToastTemplateType.toastImageAndText02,
templateContent = notifications.ToastNotificationManager.getTemplateContent(templateType),
toastMessage = templateContent.getElementsByTagName('text'),
toastImage = templateContent.getElementsByTagName('image'),
toastElement = templateContent.selectSingleNode('/toast');
// Set message & image in toast template
toastMessage[0].appendChild(templateContent.createTextNode(message || 'Demo message'));
toastImage[0].setAttribute('src', imgUrl || 'https://unsplash.it/150/?random');
toastImage[0].setAttribute('alt', imgAlt || 'Random sample image');
toastElement.setAttribute('duration', 'long');
toastElement.setAttribute('launch', '{"type":"toast","code":"info"}'); // Optional Launch Parameter
// Add actions
var actions = templateContent.createElement('actions');
templateContent.firstChild.appendChild(actions);
// Create an input box
var input = templateContent.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('title', 'Reply with');
input.setAttribute('id', 'textReply');
actions.appendChild(input);
// Create a yes button
var btnYes = templateContent.createElement('action');
btnYes.setAttribute('content', 'Yes');
btnYes.setAttribute('arguments', 'yes');
btnYes.setAttribute('launchType', 'foreground');
actions.appendChild(btnYes);
//Create a no button
var btnNo = templateContent.createElement('action');
btnNo.setAttribute('content', 'No');
btnNo.setAttribute('arguments', 'no');
btnNo.setAttribute('launchType', 'foreground');
actions.appendChild(btnNo);
// Show the toast
var toast = new notifications.ToastNotification(templateContent);
var toastNotifier = new notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.show(toast);
} else {
// Fallback
}
}
if (typeof Windows !== 'undefined' &&
typeof Windows.UI !== 'undefined' &&
typeof Windows.UI.WebUI !== 'undefined') {
Windows.UI.WebUI.WebUIApplication.addEventListener('activated', function (args) {
var activation = Windows.ApplicationModel.Activation;
if (args.kind === activation.ActivationKind.toastNotification) {
console.log(args.argument);
console.log(args.userInput.textReply);
}
});
}
createToast();
})();
@mchampaneri
Copy link

Getting Windows undefined.

if(Windows !== 'undefined' &&      
^
                                 
ReferenceError: Windows is not defined          

OS : win10 [Version 10.0.17763.973]

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