在应用程序开发中更改Windows 8吐司通知



我正在开发Windows 8应用程序,用于使用JavaScript和HTML中的Toast通知发送通知。默认情况下,吐司声音为"默认",但我想将其转换为" SMS"声音。我还从用户那里获取输入,在通知期间要显示的内容。

我的HTML代码看起来像

<div>String to display <input type="text" size="20" maxlength="20"      
id="inputString" /></div>
<button id="inputButton" class="action">button</button>

JavaScript代码看起来像

(function () {
"use strict";
var page = WinJS.UI.Pages.define("/html/home.html", {
    ready: function (element, options) {
        document.getElementById("inputButton").addEventListener("click", noti, false);
 ...

function noti(e) {
    var targetButton = e.currentTarget;

现在我被卡住了。

我有示例SDK的以下代码,我无法适合

 var toastSoundSource = targetButton.id;
    // Get the toast manager for the current app.
    var notificationManager = Notifications.ToastNotificationManager;
    var content = ToastContent.ToastContentFactory.createToastText02();
    content.audio.content = ToastContent.ToastAudioContent[toastSoundSource];

我还阅读了一些博客,说可以只使用它来完成

toast.Audio.Content = ToastAudioContent.Silent;

我想我只是把它弄乱了。请尽快帮助您

我正在检查您的代码,实际上您的问题在这里:

var toastSoundSource = targetButton.id; // you are getting the id of your button, however your button ID is not a valid index for the sounds we have available in Win8.
content.audio.content = ToastContent.ToastAudioContent[toastSoundSource]; //so when your code arrive here, nothing changes, and Winjs keeps using the DEFAULT sound...

为了解决您的问题...您可以做2件事,将按钮ID更改为" SMS"或以以下方式之一实现您的代码:

1st-强迫Windows使用SMS(如果这是您要使用的唯一声音...

 function noti(e) {
    var targetButton = e.currentTarget;
    var toastSoundSource = targetButton.id;
    // Get the toast manager for the current app.
    var notificationManager = Notifications.ToastNotificationManager;
    var content = ToastContent.ToastContentFactory.createToastText02();
    content.audio.content = ToastContent.ToastAudioContent.sms; // force system to use SMS sound
    var toast = content.createNotification();
    notificationManager.createToastNotifier().show(toast);
}

2nd-您可以创建一个If/else,如果您有更多的选项,代码可以根据单击的按钮在声音上进行选择...

function noti(e) {
    var targetButton = e.currentTarget;
    var toastSoundSource = targetButton.id;
    // Get the toast manager for the current app.
    var notificationManager = Notifications.ToastNotificationManager;
    var content = ToastContent.ToastContentFactory.createToastText02();
    if ( toastSoundSource == "inputButton") 
       content.audio.content = ToastContent.ToastAudioContent.sms;
    else 
           content.audio.content = ToastContent.ToastAudioContent.im
    var toast = content.createNotification();
    notificationManager.createToastNotifier().show(toast);
}

我希望这会有所帮助:)

最新更新