iOS 10 UserNotifications custom sound in background mode

21,496

Solution 1

Found out that alertSound doesn't have the correct file path hence the content.sound sets nothing. I was saving the recorded file in documents directory and the file path retrieve for real device is different as compared to the simulator.

Setting a sound file which is placed in project bundle did the trick

content.sound = UNNotificationSound(named: "out.caf")

Solution 2

In Swift 4.2 and Xcode 10.1

For local notifications to play sound

let content = UNMutableNotificationContent()
content.title = "Notification Tutorial"
content.subtitle = "from ioscreator.com"
content.body = " Notification triggered"
//Default sound
content.sound = UNNotificationSound.default
//Play custom sound
content.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: "123.mp3"))

"123.mp3" file needs to be in your Bundle.

Share:
21,496

Related videos on Youtube

Muhammad Umair
Author by

Muhammad Umair

A Swift enthusiast, tech hipster and a full stack iOS developer with 9+ years of experience. I have worked on different projects having divergent domains from social to medical genre apps.

Updated on April 08, 2022

Comments

  • Muhammad Umair
    Muhammad Umair about 2 years

    Since UILocalNotification is deprecated in iOS 10 so I have updated my local notification flow using the UNUserNotification framework.

    The app plays custom sound using AVPlayer when the app is in the foreground and it works fine. But in background mode when local notification is triggered, instead of custom sound, a default notification sound is being played.

    However, things were working fine in iOS9, using "didReceiveLocalNotification" method app can play custom sound even in background mode.

    Update 1:

    I'm setting local notification as follows:

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationStringForKey("reminder!", arguments: nil)
    content.body = NSString.localizedUserNotificationStringForKey("Reminder body.", arguments: nil)
    if let audioUrl == nil {
        content.sound = UNNotificationSound.defaultSound()
    } else {
        let alertSound = NSURL(fileURLWithPath: self.selectedAudioFilePath)
        content.sound = UNNotificationSound(named: alertSound.lastPathComponent!)
    }
    content.userInfo = infoDic as [NSObject : AnyObject]
    content.badge = 1
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
    let identifier = "Reminder-\(date)"
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    let center = UNUserNotificationCenter.currentNotificationCenter()
    center.addNotificationRequest(request, withCompletionHandler: { (error) in
        if error != nil {
            print("local notification created successfully.")
        }
    })
    

    I have already gone through many SO Questions but didn't get the solution.

    Any help will be greatly appreciated!

    • Papershine
      Papershine about 7 years
      Please show the code that you have tried
    • mfaani
      mfaani about 7 years
      You got it fixed...but this question may come to use in a near future:
  • Muhammad Umair
    Muhammad Umair about 7 years
    Thanks for your kind reply. But I'm using LocalNotification not push/remote. My app records a sound and save it and then fire a local notification that should be played in background mode. thanks
  • Xcodian Solangi
    Xcodian Solangi almost 6 years
    what if i am getting download tune file and how should i mention with path?
  • Xcodian Solangi
    Xcodian Solangi almost 6 years
    what if i am getting download tune file and how should i mention with path?
  • Xcodian Solangi
    Xcodian Solangi almost 6 years
    what if i am getting download tune file and how should i mention with path?
  • Muhammad Umair
    Muhammad Umair almost 6 years
    @XcodianSolangi see this gist, created for you, it might help: gist.github.com/UmairSharif/d3281d147607c825c846b345ffd072d7
  • Pe Gra
    Pe Gra almost 6 years
    Yes, that's fine. It would work if the app is in foreground. In background mode you will probably have the same issue mentioned in 4) as the path you stored the downloaded file will be different that Bundle.main
  • laka
    laka about 4 years
    For me it does not work with .mp3 files. But it works with .caf files. Luckily, there are numerous online converters for that.