Adding custom sounds to your push notifications on Android and iOS apps.
Custom sounds are a way to provide a more unique, branded experience for your app(s). You may add a custom sound with every notification you send, or you may add sound to just certain types of notifications. For instance, a publisher may wish to have their brand sound to be played when receiving notifications. Meanwhile, a social network may wish to play sounds when the user receives a message from another user to differentiate those notifications from more generic system notifications.
How to Add Custom Sounds
For Mobile Apps only.
Web Push does not support custom sounds at this time. Browsers have this "in development":
- Chrome thread: https://bugs.chromium.org/p/chromium/issues/detail?id=442131
- Firefox thread: https://bugzilla.mozilla.org/show_bug.cgi?id=1105222
1. Create Sound Files
Be sure to create sound files according to the following rules. If the device cannot find the file in question, or if the file is not in a supported format, it will fall back to the default system notification sound.
Sound Filename Recommendation
It is recommended to keep sound filenames lowercase since some platforms ignore upper case letters for sound files. Instead of
|iOS||Apple is picky about file formats. Sounds must be encoded as Linear PCM, MA4 (IMA/ADPCM), µLaw, or aLaw. Must be less than 30 seconds.|
2. Add Sound Files to App
To add sounds to notifications, you must include the sound files as resources within your app. External URLs are not supported.
Add sound files to the appropriate folder in your project depending on the SDK. If the folder does not exist, create it.
Add sound files to the appropriate location in the XCode project depending on the SDK.
|Native||Add files to the XCode project root. Make sure Add to targets is selected when adding files so that they are automatically added to the bundle resources.|
3. Send Notifications with Custom Sounds
Once you have added the sound file in the appropriate folder as described above, use the below method in the
Application class to reference the sound file and use it with every notification send.
Points to Note
- The notifications sound file should already be present in the
res/rawfolder of the project.
- Supported file types are
- The file name needs to be passed without the file extension.
Once the sound file has been added as described above, use the below method in the
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "Sound_File_Name_with_Extension"))
self.bestAttemptContent.sound = [NSString stringWithFormat:@"Sound_File_Name_with_Extension", self.bestAttemptContent.sound];
Points to Note
- The notifications sound file should already be present in the project directory.
- Supported file types are
- The file name needs to be passed with the file extension.
New sounds take a while to propagate to all users
If you've recently added a sound resource to your app, you may want to wait a few days before sending notifications using the sound. This is because it can take many days or even weeks for the majority of your users to update their apps to the latest version which contains the new sound resource.
If a user has an older version of your app without the sound resource and receives a notification that references it, they will hear only the default system notification sound.
Why is my notification not playing the custom sound file?
There are a few reasons why a sound may not play:
- Sound file has an incorrect file extension.
- Sound file is not encoded in the supported format.
- Sound file is in the incorrect location.
- Sound file is too long.
Android: Make sure that it is getting built into your APK by extracting it and making sure it is located in
If shrinking resources is enabled, you can protect sound files from being removed by creating
res/raw/ with the below code:
<resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@raw/sound_file"/>
iOS: Read more in Apple's documentation for tips on how to encode files and test them.
Why is my notification playing the default sound file?
Please make sure that you followed the setup instructions carefully and the sound file is in the correct location for the SDK.
Why is the wrong sound playing?
On Android, notifications will get grouped together after a certain amount are received by the device without opening them. Grouped notifications might sometimes play the default sound.
Updated over 1 year ago