I’m creating an Ionic 2 and Ionic Native code sample for using Local Notifications and thought I’d take a moment to write up a post about notification sounds.
Lets take a look at the typical code to create a Local Notification in Ionic 1:
In the object being passed to the schedule method you see a sound
property. This property is set to the default sound represented by 'res://platform_default'
which, of course, is the sound that the user has selected as their notification sound.
Please Note: When you are testing with the iOS Simulator, the default sound does not play. This fact could possibly lose you time, effort, hair and sanity. As always, if something isn’t working as expected in the simulator or emulator, take the time to test it on a device.
Please Note: On the Android platform your notifications will always make a sound whether your application is in the foreground or the background. On iOS, the only time you’ll see your application’s notifications is when your application is in the background.
Default sounds are great, but, hey, your app is something special and deserves its very own notification sound. Right?
In that case we make a simple change to the property:
sound: 'file://audio/notification.wav'
In this case we are using a .wav
file which works on both iOS and Android. You’ll notice we are using the file://
protocol to retrieve this file. But where do we place this file so that it can be found?
In Ionic, the file should be placed in the www
folder. That makes sense in Ionic 1 since basically everything goes there. So, using this code example the file would be located at www/audio/notification.wav
.
The code in Ionic 2 using Ionic Native might look like this:
But don’t be confused by the new Ionic 2 directory structure. The custom sound file does not belong in the app
folder. It, too, should reside in the www
folder.