Search code examples
iosios7uilocalnotification

Set a custom sound for localNotification from NSData


I need to pass a sound to localNotification. I know it's limitations and required format. But the way of adding it it's just by getting it from file name. UILocalNotification has just a property soundName where it gets the sound only by its name on the App Bundle.

I have set a SQLite Database when I save sounds in blob format and get it back to NSData. I need to set this sound which I have and give it as a parameter to localNotification in NSData format.

Here is how I set up the localNotification

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH-mm";
NSDate* date = [dateFormatter dateFromString:[dateFormatter stringFromDate:timeToSetOff.date]];

localNotification.soundName = @"";  //Here is the line where I am having issues

[localNotification setFireDate:date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Has anyone dealt with this before, or implemented this algorithm?


Solution

  • The soundName property only accepts a filename of a sound in you app s bundle. So when you app is running in the foreground you will be able to play the sound with AVplayer or something. You can not use the soundName to store the name. Also saving the sound as blobs will make it harder, just save the file in the documents directory and path to that file in your SQLite database.

    Then you pass the path to the sound file in the userInfo of the UILocalNotification.