Search code examples
iosswiftuilocalnotification

UILocalNotification Sound Not Working


I have some problems with sound of UILocalNotification. I have the user permissions in app delegate.

It's my code:

var nottypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
var notsettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: nottypes, categories: nil)
application.registerUserNotificationSettings(notsettings)

Code for notification:

var notification:UILocalNotification = UILocalNotification()
notification.alertBody = "Simple"
notification.alertTitle = "Back To App!"
notification.fireDate = NSDate(timeIntervalSinceNow: 20)
notification.soundName = "Chord"
UIApplication.sharedApplication().scheduleLocalNotification(notification)

Solution

  • You should always take a look at the documentation before posting a question at S.O. If you read the first line of the definition of soundName's property it says:

    For this property, specify the filename (including extension) of a sound resource in the app’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound. When the system displays an alert for a local notification or badges an app icon, it plays this sound. The default value is nil (no sound). Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead.

    So try:

    notification.soundName = "Chord.mp3"   // use .mp3 or your file extension if it is not an mp3