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.
How to put file there?
Follow Apple documentation for preparing custom sound file for your app.
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.
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:
{
"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 needed
option selected while drag and drop. The blacked out part has project name.
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.
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.