Local Notification sound not came when App in foreground iOS Swift

Solution:

 If app is in foreground the notification sounds will not be played.So you have to manually play the sound to notify the user.Use the below code to play the sound


    var sound:AVAudioPlayer!
                        if appstate.applicationState == .active {
                            let sound = "soundname"
                            let alertSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "mp3")!)
                            do {
                                self.sound = try AVAudioPlayer(contentsOf: alertSound as URL)
                                self.sound.prepareToPlay()
                                DispatchQueue.main.async {
                                    self.sound.play()
                                }
                            } catch {

                            }
}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift