toast 上的自定义音频Custom audio on toasts

Toast 通知可使用自定义音频,从而让你的应用展现品牌的独特音效。Toast notifications can use custom audio, which lets your app express your brand's unique sound effects. 例如,消息应用可在其 Toast 通知上使用自己的消息传递声音,而不是使用通用的通知声音,以便用户立即知道接收的通知来自该应用。For example, a messaging app can use their own messaging sound on their Toast notifications, so that the user can instantly know that they received a notification from the app, rather than hearing the generic notification sound.

安装 UWP 社区工具包 NuGet 程序包Install UWP Community Toolkit NuGet package

若要通过代码创建通知,我们强烈建议使用 UWP 社区工具包通知库,它提供通知 XML 内容的对象模型。In order to create notifications via code, we strongly recommend using the UWP Community Toolkit Notifications library, which provides an object model for the notification XML content. 可手动构造通知 XML,但此操作比较杂乱且容易出错。You could manually construct the notification XML, but that is error-prone and messy. UWP 社区工具包内部的通知库由 Microsoft 拥有通知的团队生成和维护。The Notifications library inside UWP Community Toolkit is built and maintained by the team that owns notifications at Microsoft.

从 NuGet 安装 Microsoft 工具包Install Microsoft.Toolkit.Uwp.Notifications from NuGet.

添加命名空间声明Add namespace declarations

Windows.UI.Notifications 包括磁贴和 Toast API。Windows.UI.Notifications includes the Tile and Toast API's. Microsoft.Toolkit.Uwp.Notifications 包括通知库。Microsoft.Toolkit.Uwp.Notifications includes the Notifications library.

using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;

添加自定义音频Add the custom audio

Windows 移动版始终支持 toast 通知中的自定义音频。Windows Mobile has always supported custom audio in Toast notifications. 但是,只有版本 1511(内部版本 10586)的桌面设备添加了对自定义音频的支持。However, Desktop only added support for custom audio in Version 1511 (build 10586). 如果将包含自定义音频的 toast 发送到版本 1511 之前的桌面设备,该 toast 将处于静音模式。If you send a Toast that contains custom audio to a Desktop device before Version 1511, the toast will be silent. 因此,对于版本 1511 之前的桌面设备,toast 通知中不应包含自定义音频,这样通知至少会使用默认通知声音。Therefore, for Desktop pre-Version 1511, you should NOT include the custom audio in your Toast notification, so that the notification will at least use the default notification sound.

已知问题:如果使用版本 1511 的桌面设备,只有在通过 Microsoft Store 安装应用时,自定义 toast 音频才能正常工作。Known Issue: If you're using Desktop Version 1511, the custom toast audio will only work if your app is installed via the Store. 这意味着在提交到 Microsoft Store 之前无法在本地测试桌面设备上的自定义音频,但只要从 Microsoft Store 安装应用,音频便可正常工作。That means you cannot locally test your custom audio on Desktop before submitting to the Store - but the audio will work fine once installed from the Store. 我们已在周年更新中修复了此问题,因此本地部署的应用中的自定义音频将正常工作。We fixed this in the Anniversary Update, so that custom audio from your locally deployed app will work correctly.

var contentBuilder = new ToastContentBuilder()
    .AddText("New message");

    
bool supportsCustomAudio = true;
 
// If we're running on Desktop before Version 1511, do NOT include custom audio
// since it was not supported until Version 1511, and would result in a silent toast.
if (AnalyticsInfo.VersionInfo.DeviceFamily.Equals("Windows.Desktop")
    && !ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
{
    supportsCustomAudio = false;
}
 
if (supportsCustomAudio)
{
    contentBuilder.AddAudio(new Uri("ms-appx:///Assets/Audio/CustomToastAudio.m4a"));
}

// TODO: Send the toast

支持的音频文件类型包括...Supported audio file types include...

  • .aac.aac
  • .flac.flac
  • .m4a.m4a
  • .mp3.mp3
  • .wav.wav
  • .wma.wma

发送通知Send the notification

发送带有音频的通知与发送定期通知相同。Sending a notificaiton with audio is the same as sending a regular notification. 请参阅 发送本地 toast 以了解详细信息。See Send local toast to learn more.