Change push notification sound
Asked Answered
A

2

35

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?

Aniakudo answered 31/5, 2016 at 3:58 Comment(2)
follow this link: #25478895Encarnacion
FWIW ANY sound (system sounds or your very own made sound) other than default is considered a custom sound and MUST be manually added to AppBundle.Ostend
V
54

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.

enter code here

Vaclav answered 31/5, 2016 at 4:12 Comment(12)
How to I can do this:Aniakudo
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.Aniakudo
Just drag and drop your file in your project. When prompt appear, make sure you have Copy items if needed option selected.Vaclav
I had add sound file to bundle resource: pasteboard.co/1kc85H2s.png and custom sound name on the server.Aniakudo
Was the formatting of file done according to the steps specified in Apple Documentation mentioned above?Vaclav
Make sure the sound file is inside the bundle (i.e inside the hierarchy of your project) and not outside it.Vaclav
I had convert from wav to caf, but it still not working.Aniakudo
oh my god. I tested by terminal, it's working. But, when I using server to send push notification then it's not working.Aniakudo
I will discuss with my server to change file name.Aniakudo
Log the payload that you are receiving for the server and cross check it with the payload that you used while testing from terminal.Vaclav
where is payload, how to create payloadBodywork
Can pick sound from document Directory ?Ssm
A
25

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.

Airfoil answered 31/5, 2016 at 4:28 Comment(10)
You are right! - "Check if your sound file exists under 'Copy bundle Resources'. If not, click + button to add the sound file". ThanksMonochromat
I've tested on iOS 10.3 and sound is working with aiff, without converting it to caf.Orthogenesis
@MansurovRuslan Thats great. Was this answer useful to you in any other way ?Airfoil
@Susil, Thank you so much for your wonderful answer thats make reduce my work time and planningPasol
where is payload, how to create payload?Bodywork
@iOSDeveloper payload is created on backend. You receive it in notification. You can try to print the notification data when you receive a push.Airfoil
but how can I put sound name in payloadBodywork
what if i am get file from internet and downloading it in a folder then want to specify path and sound name how this can be done any example?Mushy
@iOSDeveloper sound name is sent by backend. You just need to keep the file with same name in your project bundle.Airfoil
it worked on React Native !! i put the sound asset inside root/src/assets. Then add the sound asset to Copy bundle Resources section. Thanks !Heyerdahl

© 2022 - 2024 — McMap. All rights reserved.