项目中用到了自定义推送声音,于是就看了下官方文档。 官方文档上说:
Preparing Custom Alert Sounds
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 系统来播放的,所以对音频数据格式有限制,可以是如下四种之一:
- Linear PCM
- MA4 (IMA/ADPCM)
- µLaw
- aLaw
对应音频文件格式是 aiff
,wav
,caf
文件,文件也必须放到 app 的 mainBundle
目录中。
可以使用 afconvert
工具来处理音频文件格式,在终端中敲入如下命令就可以将一个 mp3
文件转换成 caf
文件:
afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/test.caf -d ima4 -f caff -v |
转换完成后就可以将 test.caf这个文件拖入 Xcode 工程中,编译运行项目在真机上。
然后在极光发送推送通知时,只需配置 sound
字段的值为导入到工程中的音频文件名,这里即就是 test.caf
。如图所示。
测试,收到推送时,通知声音就是我们自定义的声音了。