当微软吐司通知按钮被按下时,如何在javascript中运行代码?

问题描述 投票:0回答:1

我想做一个有按钮或所谓的 "动作 "的microsoft toast通知,在这段代码中,我有3个按钮A,B,C,我想检查它们中的任何一个是否被点击,如果是,则运行一行代码,例如,如果A被点击,则运行debug.log('A'),我怎么能做到这一点呢?

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);
};
visual-studio-2017 toast winjs
1个回答
0
投票

对于UWP应用程序来说,当通知按钮被按下时,就会出现以下情况 activated 事件将被触发。在javascript中,可以这样写。

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

如果你使用WinJs来构建你的应用程序,你可以找到相应的代码块来处理Toast通知的点击。app.onactivated 事件 main.js

...
else if (args.detail.kind === activation.ActivationKind.launch) {
    if (args.detail.arguments) {
    }
    ...
}
...
© www.soinside.com 2019 - 2024. All rights reserved.