更改推送通知声音

Change push notification sound

如何为推送通知使用自定义声音?

根据我的研究和阅读,我发现 payload 的文件名应该在应用程序包中或应用程序数据容器的 Library/Sounds 文件夹中。

如何将文件放在那里?

按照 Apple documentation 为您的应用准备自定义声音文件。

For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.

Custom alert sounds are played by the iOS system-sound facility, so they must be in one of the following audio data formats:

Linear PCM MA4 (IMA/ADPCM) µLaw aLaw You can package the audio data in an aiff, wav, or caf file. Then, in Xcode, add the sound file to your project as a nonlocalized resource of the app bundle or to the Library/Sounds folder of your data container.

You can use the afconvert tool to convert sounds. For example, to convert the 16-bit linear PCM system sound Submarine.aiff to IMA4 audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v You can inspect a sound to determine its data format by opening it in QuickTime Player and choosing Show Movie Inspector from the Movie menu.

Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

创建文件后,最简单的方法是将其放入应用程序包中。

然后,当您发送推送通知时,只需在 JSON 负载中添加文件名。示例:

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    }
}

就是这样!您不必在应用程序代码中做任何特殊的事情。

编辑:
请将文件放入您的项目包中(即在项目层次结构中)并在拖放时选择 Copy items if needed 选项。涂黑部分有项目名称。

您的服务器管理员将在通知负载中向您发送声音名称。 有效载荷将如下所示

{
    aps =     
    {
        alert = "notification message";
        sound = "example.caf";
    };
}

您需要将声音文件添加到应用程序包中。格式应该是 .caf 。要将声音文件转换为 .caf,请尝试 运行 在终端中执行此命令。

afconvert -f caff -d aacl@22050 -c 1 sound.aiff soundFileName.caf

文件保存在桌面上。现在将您的文件拖放到您的项目中。然后 select 在目标中构建阶段。

检查您的声音文件是否存在于“复制捆绑资源”下。如果没有,请单击 + 按钮添加您的声音文件。负载中的声音名称应与您的声音文件名相同。

现在您已准备好播放自定义通知声音。