您的位置:首页 > 移动开发 > IOS开发

iOS学习爬坑记录14:自定义远程推送通知铃声(Custom Alert Sounds)

2015-03-09 10:26 211 查看
最近在调试APP的远程推送,需要做成类似微信视频通话或者语音通话的通知铃声。找了好多资料才知道怎么自定义铃声:

官方文档中有铃声格式和长度的说明:(见https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6)


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 must be in the main bundle of
the client app.

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.

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

因为我做远程推送实验是参考了其他大牛在本机上写的php文件模拟的服务器,自定义铃声也是在php文件中修改(服务器发给APNS的消息中)所以php文件节选如下:

// Create the payload body

$body['aps'] = array(

'alert' =>
'逗你玩!哈哈。',

'sound' =>
'Synth222.caf',

'badge' =>
8

);
这里面的sound键值就是我的自定义铃声。
把制作好的铃声添加到项目的main bundle中后,还有关键的一步,就是在项目设置中Build Phases-》Copy Bundle Resources中添加你添加的声音文件(我这里是Synth222.caf),铃声不能超过30s,否则系统会启用默认铃声(default)。

关于php文件的详细内容和远程推送详细过程可以参见:http://blog.csdn.net/shenjie12345678/article/details/41120637,谢谢原作者的分享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: