3

I am coding in Swift and trying to make notifications appear. They do appear but sound is not playing. I did look around this site and other sites, and my code looks the same as the proposed solutions, so I'm not sure what's wrong.

This is my AppDelegate.swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

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

    return true
}

and

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var alert = UIAlertView()

    alert.title = "Alert"
    alert.message = notification.alertBody
    alert.addButtonWithTitle("Dismiss")

    alert.show()
}

And this is the function in my ViewConrtoller.swift:

override func viewDidLoad() {
    super.viewDidLoad()
    var localNotification: UILocalNotification = UILocalNotification()

    localNotification.soundName = "sample.wav";
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 2)
    localNotification.alertAction = "TEST"
    localNotification.alertBody = "Sound Test"
    localNotification.applicationIconBadgeNumber = 1

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
Lorenzo
  • 3,293
  • 4
  • 29
  • 56
user5405324
  • 31
  • 1
  • 2
  • What are some of the posts that you looked at? Can you post some links? – Caleb Kleveter Oct 03 '15 at 21:12
  • You need to read the [Preparing Custom Alert Sounds](https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6) section of the [Local and Remote Notification Programming Guide](https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1). – rmaddy Oct 03 '15 at 21:18
  • I read the Preparing Custom Alert Sounds and converted the file to a .caf file, and added the file to my project (Created Resources -> Audio folder in the folder), but it still isn't working. And some of the posts are like this http://stackoverflow.com/questions/26734350/local-notification-sound-not-working and this http://czetsuya-tech.blogspot.com/2014/08/how-to-implement-local-notification-in.html#.VhBOubSrQ1j – user5405324 Oct 03 '15 at 21:55

2 Answers2

1

After looking around some I found a few answers that might help. The #1 thing that seemed to be the issue was having the sound turned off on the device, see the answers to this and this post. Another solution was to change the audio file format, even though the file type was correct, changing it solved the problem. You also might find this post helpful, but none of the answers were accepted. And, as rmaddy mentioned, you might want to take a look at the Apple docs:

For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.

Custom alert sounds are played by the iOS system-sound facility, so they must be in one of the following audio data formats:

Linear PCM MA4 (IMA/ADPCM) µLaw aLaw You can package the audio data in an aiff, wav, or caf file. Then, in Xcode, add the sound file to your project as a nonlocalized resource of the app bundle or to the Library/Sounds folder of your data container.

You can use the afconvert tool to convert sounds. For example, to convert the 16-bit linear PCM system sound Submarine.aiff to IMA4 audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v You can inspect a sound to determine its data format by opening it in QuickTime Player and choosing Show Movie Inspector from the Movie menu.

Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

Edit:

About the main bundle:

Each application has a main bundle, which is the bundle that contains the application code. When a user launches an application, it finds the code and resources in the main bundle that it immediately needs and loads them into memory. Thereafter, the application can dynamically (and lazily) load code and resources from the main bundle or subordinate bundles as required.

The NSBundle class and, for procedural code, the CFBundleRef opaque type of Core Foundation give your application the means to locate resources in a bundle. In Objective-C, you first must obtain an instance of NSBundle that corresponds to the physical bundle. To get an application’s main bundle, call the class method mainBundle. Other NSBundle methods return paths to bundle resources when given a filename, extension, and (optionally) a bundle subdirectory. After you have a path to a resource, you can load it into memory using the appropriate class.

To see this information click on bundle, then click on 'more...'

enter image description here

Edit 2:

Try adding you audio file where the code files are, no fancy folders for the audio, just slap it in there.

Each application has a main bundle, which is the bundle that contains the application code. When a user launches an application, it finds the code and resources in the main bundle that it immediately needs and loads them into memory. Thereafter, the application can dynamically (and lazily) load code and resources from the main bundle or subordinate bundles as required.

So put your audio file in the main bundle, and see if it works.

Community
  • 1
  • 1
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • Thanks! I did try the file conversion (using the exact same code they gave as an example with the submarine sound), which is only 1 second long. However, I'm confused about what the "main bundle of the client app" means? I tried "File > Add Files to" and that didn't work, and then I tried moving the caf file into the folders where my code was. – user5405324 Oct 03 '15 at 22:00
  • @user5405324, I added more information the my answer, does it make sense, or do you need to know more about accessing the main bundle? – Caleb Kleveter Oct 03 '15 at 22:15
  • I need a little more information. I called my project "LocalNotification" and created a Resources folder in the same folder as the LocalNotification and LocalNotificationTests folders and the Resources folder is what contains sub.caf, but the sound still doesn't sound when I test it in an iOS Simulator. – user5405324 Oct 03 '15 at 22:28
  • Can you test on a device? If so, does it make the noise? – Caleb Kleveter Oct 03 '15 at 22:42
0

Also check that the sound file is included in "Target Membership" which you find in the right-hand-pane of information about the file in Xcode. Above that is where you'd be able to see its "Full Path". When you added the file, you might have forgotten to include it in the target(s) you need, so even though it is in the project archive, it isn't in the target's bundle and so it can't be found.

Howard Cohen
  • 135
  • 1
  • 6