Using notification sounds on iOS and Android
When implementing a push solution it is critical to think about all the small details that will make your app be more usable and stand out.
We have often seen that customizing notification sound for iOS or Android apps is often simply ignored. When this happen, the application will use the default sound, which is often the same than the sound of SMS received.
For your users, this is a great cause of confusion that lead to action to solve their issue:
- They may disable sound just because they cannot know if this the alert they just had is a sound coming from your app or is a new message.
- They may disable push totally, removing potentially important features to your application.
- They may also decide that your app is annoying and uninstall the application.
This article will help you implement custom sound for push notification in your application.
Implementing custom push notification sounds on iOS
Implementing custom sound on iOS is often documented, but it is always good to remember the basics.
Preparing the sound file
Remember that the default sound is played when:
- The sound file is not found
- The sound file is invalid. This is the case for example if the sound you are trying to play is longer than 30 seconds.
The file is also invalid if it is not in one of the expected extension: aiff, caf or wav.
It means you need to prepare a sound valid file to be able to play a sound different from the default.
For more details on how to prepare a custom sound for iOS, you can read Apple documentation: Preparing Custom Alert Sounds
Adding the sound in iOS application bundle
iOS can only play a sound that is packaged and delivered in the App Store build. There is no way to download a sound from the network at runtime or to update the sound list after the app has been released. You cannot download a sound from your app to play it when receiving a notification.
This makes it another good reason to address the sound question from the beginning, as it cannot be added without getting a new version of the application approved by Apple.
To implement push in you application:
- Add the sound file to your project, for example by dragging it to your resource folder. Make sure you check the box to copy it there to commit it in your repository. Here, adding pulses.wav:
- Check that the sound file is properly added to your bundle resources during build phase. Here pulses.wav:
- Rebuild you application
That should be enough to allow you to play a custom sound on a notification.
Implementing custom push notification sounds on Android
Preparing sound file
As for iOS, you need to embed the sound file in the Android application package to be able to play that sound when receiving a notification.
Here are a few things to be careful about when preparing Android sound file:
- Android does not support sound files that are not valid Java identifier. It means that your sound file cannot be named ‘1.caf’ for example. Android build process map resources to class properties in the Resource class (R.class) or any of its inner classes, like R.color, R.layout, R.string, R.raw, etc. As R.raw.1 is not not a valid property 1.caf will not be a valid name for sound file.
- Android supports different formats than iOS. We recommend for example, you use linear PCM Wave file with .wav extension. It should then work fine on both systems.
Packaging the sound and playing it with Boxcar Android SDK
On Android, Google Cloud Message (GCM) is used to deliver the alert to the device. Then, as a developer, it is up to you to prepare the notification to display, play sound, etc.
That’s where the Boxcar SDK play an important role. We offer the option to do the right thing on your behalf, making push notification a pleasure on Android.
In Boxcar Android SDK, we made the default behaviour work the same as iOS so that regarding push sound, you can offer a consistent behaviour to your users, no matter which version they use.
When a push arrives, the SDK checks the payload content to know if it should play a sound when the notification is sent to the Android Notification center. The following rule is followed by the SDK:
- if the payload doesn’t define a sound file to play, it doesn’t play any sound (silent push)
- if the payload defines a sound file that can’t be found on the application, it plays the default system sound
- otherwise, it plays the sound defined in the payload
As we will see further, the push data can contain a reference to a sound file to play. The idea is that you application can have multiple sounds, one for urgent notifications, another for normal content, etc. In that case, the person that publishes the push just tells what is the sound that should be played when the message arrives at the user’s device.
Every custom sound file needed (any other than the system’s default) should be packaged along with the mobile application. You should simply put the sound file under res/raw folder of your project. If an incoming push tells to play ‘beep’ sound, then the SDK will locate a sound resource with that name in the generated R.raw inner class, which maps to a file on res/raw folder (for example R.raw.beep resource identifier refers to ‘res/raw/beep.wav’ file) and plays it. If the file can’t be located, the system’s default is played instead.
That should be it !
Please, refer to our Boxcar Android SDK documentation for more details: Configuring the sound to play on incoming notifications
Sending push notification with custom sound with Boxcar Developer Push
Defining sound to play from Boxcar Push Console
All you have to do is put the file name of the sound you want to play in the form:
This will work both on iOS and Android devices.
Defining sound to play from Boxcar Publisher API
When sending a JSON payload to trigger a push through the publisher API, all you have to do is to include a sound name in in the aps section:
This will work on iOS and Android devices and trigger a sound on both platform.
We are just scratching the surface of what you can do with Boxcar Developer Push and Boxcar SDK. We will keep on writing new articles that show and compare how to implement the same feature on both iOS and Android.
If you have questions on this article or on other push notification topics, feel free to get in touch on Boxcar API Uservoice.