ios 更改推送通知声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37536591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:12:03  来源:igfitidea点击:

Change push notification sound

iospush-notificationapple-push-notifications

提问by user5068763

How can I use a custom sound for push notifications?

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

According to my research and reading, I have found that the payload should have file name that is in the app bundle or in the Library/Sounds folder of the app's data container.

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

How to put file there?

如何将文件放在那里?

回答by Rohan Sanap

Follow Apple documentationfor preparing custom sound file for your app.

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

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.

对于 iOS 中的远程通知,您可以指定 iOS 在为应用程序显示本地或远程通知时播放的自定义声音。声音文件可以位于客户端应用程序的主包中,也可以位于应用程序数据容器的 Library/Sounds 文件夹中。

自定义警报声音由 iOS 系统声音工具播放,因此它们必须采用以下音频数据格式之一:

线性 PCM MA4 (IMA/ADPCM) μLaw aLaw 您可以将音频数据打包为 aiff、wav 或 caf 文件。然后,在 Xcode 中,将声音文件作为应用程序包的非本地化资源添加到您的项目或数据容器的 Library/Sounds 文件夹中。

您可以使用 afconvert 工具来转换声音。例如,要将 16 位线性 PCM 系统声音 Submarine.aiff 转换为 CAF 文件中的 IMA4 音频,请在终端应用程序中使用以下命令:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v 您可以通过在 QuickTime Player 中打开声音并从电影中选择 Show Movie Inspector 来检查声音以确定其数据格式菜单。

播放时自定义声音必须在 30 秒以下。如果自定义声音超过该限制,则改为播放默认系统声音。

Once you have made the file, easiest way is to put it in app bundle.

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

The, when you send push notification, just add the name of file in JSON payload. Example:

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

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

Thats it! You don't have to do anything special in code of app.

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

Edit:
Please put the file inside your project bundle (i.e inside the hierarchy of project) and have Copy items if neededoption selected while drag and drop. The blacked out part has project name.

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

enter code here

在此处输入代码

回答by Sushil Sharma

Your server administrator will send you the sound name in notification payload. Payload will look like this

您的服务器管理员将在通知负载中向您发送声音名称。有效载荷看起来像这样

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

You need to add sound file to app bundle. And format should be .caf . To convert you sound file to .caf, try to run this command in terminal.

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

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

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

File is saved on desktop. Now Drag and drop you file to your project. Then select build phase in targets.

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

Check if your sound file exist under 'Copy bundle Resources'. If not, click + button to add your sound file. Name of sound in payload should be same as your sound file name.

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

Now you are all set to play custom notification sound.

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