在应用程序开发中更改Windows 8 Toast Notification声音

时间:2012-10-25 09:18:27

标签: notifications windows-8 microsoft-metro toast windows-applications

我正在使用JavaScript和Html中的toast开发用于消息传递通知的Windows 8应用程序。 由于默认情况下吐司声是“默认”,但我想将其转换为“短信”声音。 我也在收集用户的意见,以便在通知期间显示什么。

我的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;

我想我只是搞砸了。请尽快帮助。谢谢你

1 个答案:

答案 0 :(得分:0)

我正在检查你的代码,确实你的问题就在这里:

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...

为了解决您遇到的问题......您可以做两件事,将按钮ID更改为“短信”或通过以下方式之一实现您的代码:

1st - 强制窗口使用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);
}

第二 - 你可以创建一个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);
}

我希望这会有所帮助:)