Play custom push notification sound iOS

Push notification is one of useful feature that are now common in many apps. It helps the user to notify about the things when user are not in interaction with the apps. Push notification helps app to interact with the users.

When devices are registered for push notification, they can receive push notifications. When they receive notification, it can play sound, send messages, show badge value etc. The sound in notification plays important role to notify user. Push notification plays default sound when received. However apple also let developers to customise the push notification sound they play in app. You might have noticed in most of the app that it plays different sound when received push. Playing suitable custom sound according to the nature of app make push notification more interesting. Isn't it? We can change the default push notification sound easily.

For this we will need a sound file that we want to play when push notification is received. The sound that plays should be short one i.e less than 30 sec and less in size. If the sound is more than 30 sec then default sound is played automatically instead of custom sound. We can package the audio data in an either aiff, wav, or caf file. Other formats like .mp3 is currently not supported. You can convert the files to either .aiff, .wav or .caf file.

In order to play custom sound we need to add the sound file in xcode in project. Then the push notification payload should be renamed for default sound key-value with the custom sound file name.

for eg. if the push payload is as below which plays default sound.

{
“aps” : {
“alert” : “Your message here.”,
“sound” : “default”,
“badge” : 9
}
}

In order to play custom sound say named mysound.caf, that name should be updated in the payload as :

{
“aps” : {
“alert” : “Your message here.”,
“sound” : “mysound.caf”,
“badge” : 9
}
}

The server sending push notification should change the value of “sound” key in payload with actual sound file name.

And also the sound file mysound.caf should be added to Xcode project file, as shown like below with exact name.

In Xcode, add the sound file to your project as a non localized resource of the application bundle.

Now send push notification to the device, then you should hear different push sound notification than a default sound.

Thank You!

#Happycoding

UPDATE :

From Xcode 11.4 and later, we can test push notification on simulator itself. This makes us easy to simulate and test custom push sound on simulator as well.

Download the sample Xcode Project from this github link
PushTestOnSimulatorSampleSwift

Run the project and tap on Allow button for enabling push notification on simulator.

Then open apns file samples folder attached with the sample project and just drag and drop the customsoundapns.apns file on simulator. You will hear custom push sound played. This makes us conform that the sound file is working and push will work when server sends the push to devices with sound name in the payload. Sound files are in the Sounds folder in Xcode project bundle.

You can also drag and drop sample.apns file. It will just play default sound as its “sound” key have value as “default”.

You can find 3 supported sound formats for push, .caf, .aiff, .wav. Also notice that all sound duration are less than 30 sec.

You can change sound key value to any of the sound file and test. For testing on you project don’t forget to set the project bundle identifier to your project for which you are testing on .apns file at top.

For more details about push notification on simulator refer to this post: https://medium.com/@prakash.maharjan/testing-push-notification-on-ios-simulator-swift-4fa943bee101

Thank You!

Result oriented freelance iOS Developer (Swift & Objective C), passionate about app development. Available for freelance ios app development works.