1

I am trying to make a microsoft toast notification that has buttons or so called "actions" in this code i have 3 buttons A,B,C and i want to check if any of them are clicked and if so the run a line of code just for example if A is clicked then run debug.log('A') how can i do that? (im new to visual studio and javascript)

document.addEventListener('keydown', logKey);

function logKey(e) {
    var notifLib = Microsoft.Toolkit.Uwp.Notifications;

    var toastContent = new notifLib.ToastContent();
    var toastVisual = new notifLib.ToastVisual();
    var toastBindingGeneric = new notifLib.ToastBindingGeneric();

    var adaptiveText = new notifLib.AdaptiveText();
    adaptiveText.text = "Hello World";
    toastBindingGeneric.children.push(adaptiveText);

    adaptiveText = new notifLib.AdaptiveText();
    adaptiveText.text = "This is a simple toast message";
    toastBindingGeneric.children.push(adaptiveText);

    toastVisual.bindingGeneric = toastBindingGeneric;

    toastContent.visual = toastVisual;

    var toastActionsCustom = new notifLib.ToastActionsCustom();

    var toastButton = new notifLib.ToastButton("a", "action=at&userId=1");
    toastButton.activationType = notifLib.ToastActivationType.background;
    toastActionsCustom.buttons.push(toastButton);

    toastButton = new notifLib.ToastButton("b", "action=b&userId=1");
    toastButton.activationType = notifLib.ToastActivationType.background;
    toastActionsCustom.buttons.push(toastButton);

    toastButton = new notifLib.ToastButton("c", "action=b&userId=1");
    toastButton.activationType = notifLib.ToastActivationType.background;
    toastActionsCustom.buttons.push(toastButton);

    toastContent.actions = toastActionsCustom;

    // Create the toast notification
    var toastNotif = new Windows.UI.Notifications.ToastNotification(toastContent.getXml());

    // And send the notification
    Windows.UI.Notifications.ToastNotificationManager.createToastNotifier().show(toastNotif);
};
1

1 Answer 1

0

for UWP applications, when the notification button is pressed, the activated event of the application will be triggered. In javascript, it can be written like this:

Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (activatedEventArgs) {
     if (activatedEventArgs.kind == Windows.ApplicationModel.Activation.ActivationKind.toastNotification) {
          console.log(activatedEventArgs.argument);
     }
});

If you are using WinJs to build your application, you can find the corresponding code block that handles the click of the Toast notification in the app.onactivated event in main.js

...
else if (args.detail.kind === activation.ActivationKind.launch) {
    if (args.detail.arguments) {
    }
    ...
}
...
Sign up to request clarification or add additional context in comments.

9 Comments

the problem im having is that i dont know how to use it on a specific button cause i have 3 Toastbuttons so how would i be able to specify like if toastbutton A is pressed ? (sorry if this looks dumb im really new to javascript)
When you defined ToastButton, you gave a series of parameters (such as action=a&userId=1). When you click the button A, the actived event of the application will be triggered, and this parameter will be passed in (args.detail.arguments). You can determine which button was pressed according to the incoming parameters in the actived event callback (such as Different actions to judge)
` Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (activatedEventArgs) { if (activatedEventArgs.kind == Windows.ApplicationModel.Activation.ActivationKind.A) { } });` it wont run did i misunderstand something ?
i changed the definiton of the A toastbutton to only be A and not action=a&userId=1
I don’t quite understand what you mean by only be A. When you defined ToastButton, you passed in two parameters, one is the display content of the Button, and the other is the parameter attached to the Button. After the application is activated, parameters will be passed in, so we need to set some identifiers in the parameters, such as action=A. But before that, you may consider setting toastButton.activationType to notifLib.ToastActivationType.foreground, which will activate the application.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.