How To Set UILocalNotification Soundname in Swift - ios

We are making an alarm clock app and want to have our UILocalNotification sound be an alarm noise. Currently we can only get the UILocalNotificationDefaultSound to work for us.
We are trying to do something like this:
localNotification.soundName = "iphonesongw.wav"
Where iphonesongw.wav is in our project.

Here is what I've read from Apple's Doc:
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.

Related

Add System Sound to iOS remote notification in UNNotificationServiceExtension

I am successfully modifying my remote notification payload using the UINotificationServiceExtension.
I want to change the alert sound based on the user's choice here. In order to do that I need to assign a UINotificationSound object to the bestAttemptContent:
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "alertTone.caf"))
This will work for tones which I have created and imported into my project. One named alertTone.caf in the above example.
However, I want to use the system alert tones instead of my own. I know I can set the default tone but I want access to other custom tones. Is there anyway at all to do this?
You can't reference the system sounds for UNNotificationSound, but you can try to play the sound when you receive the notification in the service extension.
// import
import AVFoundation
// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016
// Play the correct sound when the notification is received in the service extension
AudioServicesPlaySystemSound (systemSoundID)
Source for playing system sounds here.
This is a list of all the system sound ids that you can use for reference.

How to prevent the app from going to sleep, in order to integrate a sleep mode for the music

I have a working music App, written in Swift.
I wanted to integrate a sleep mode which could pause the music played after, say 10 minutes.
I used perform(_ aSelector: Selector, with anArgument: Any?, afterDelay delay: TimeInterval) for this task, it worked fine.
But there's a problem. There's a good chance that the iPhone'll go to sleep before the 10 minutes end, so the pause fonction isn't called, and the music continues to play.
I understand that there are other ways to send an action after 10 minutes (NSTimer, use GCD) but if I understand, none of this solutions will prevent the machine to go to sleep, or wake up the app to perform the task.
Perhaps I could use background mode, but:
My goal isn't really in the problems that this mode is supposed to solve. I don't want to have some time to finish, I just want a little 1/10 of second processor at a certain time.
This option don't guarantee to work for a 10 minutes period.
Is this a way to achieve what I need?
You can do: UIApplication.shared.isIdleTimerDisabled = true, as this is the only way in Swift 3, and you could do UIApplication.sharedApplication().idleTimerDisabled = truein Swift 2 or [UIApplication sharedApplication].idleTimerDisabled = YES; in objective-c

Get the soundName by soundID to assign it to a UILocalNotification

I have a UILocalNotification, and I don't want to set it to use a default tone. I have used AudioToolbox.framework, and I have a SystemSoundID by referring to this library: https://github.com/TUNER88/iOSSystemSoundsLibrary.
But UILocalNotification needs a sound name. How can I get the name from the SystemSoundID?
Description of soundName in UILocalNotification Class Reference mentions
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.
So if you want the default local notification sound use UILocalNotificationDefaultSoundName.
Also, a note from Multimedia Programming Guide
Note: System-supplied alert sounds and system-supplied user-interface
sound effects are not available to your application.
So you cannot use any system sound id you find in iOSSystemSoundsLibrary for this.
If you want a custom sound, you'll have to provide your own.

How to add sound for UILocalNotification?

I have done with basic notification for my App. I want to add sound effect to my notification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [NSDate date];
//localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = msg;
localNotification.soundName = #"Morse.aiff";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
How to add sound effect?
You may want to start with reading Preparing Custom Alert Sounds of the Local and Push Notification Programming Guide (make sure that your custom sound is shorter than 30s).
If you want to use the default sound: use UILocalNotificationDefaultSoundName (as suggested by Euroboy)
Update: You seem to be not the only person experiencing this problem. A possible (but of course not a good) solution is described here.
In my case the problem was that I forgot to register for UIUserNotificationTypeSound. Without registration to UIUserNotificationTypeSound your App will not have the 'Sounds' option available under the Notifications Settings.
Here is the code snippet from the Local and Remote Notification Programming Guide (tilo already added a link to it above).
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
After adding that code you can enable the sound option in Notification Settings for your App.
I had similar problem while trying to play mp3 file as local notification sound.
Sound was shorter than 30 seconds, it was bundled, everything appears regular on the surface.
After few hours i have realised that problem was caused by online mp3 cutter tool.
I have find better editor, and file became playable on notification.
If mp3 file is edited with some tool, it is possible that something was missed by edit tool.
May be your problem is also caused by sound editor, or sound format converter.
I found really simple solution for playing custom sound in local notification.
1. Normally sound will be work perfectly with aiff format.
2. When you added custom sound, please check if this sound added to the "Build Phases"
3. You already added your custom sound but basic system alert playing again? - delete application from your device and reboot. After connect your device and compile application again.
4. Profit! it will be work!
PS: This is solution if you have iOS 10. How i am understanding this is system bag..
You need to make sure the resource file has your Target membership selected. For my case, it was not checked, and notification was still the system default one.

(iOS) How can I speed up the reaction time of "didReceiveLocalNotification:"?

Background
I am in the early stages of learning iOS Development (and Objective C). After the latest tutorial I followed, I decided to take the lesson a little further.
After the tutorial, I had an Alarm Clock application that fired a LocalNotification at a time set by the user. Since the local notification only played the alarm sound while the app was in background mode, I didn't think it was much of an alarm clock. So I set out to add alarm functionality while the app was open and in focus.
Where I am now
My alarm clock application functions exactly as it should. If the app is open, the user receives a UIAlertView and the alarm sound.
The Problem
To trigger the UIAlertView, I am using the didReceiveLocalNotification: method.
There is a fairly big delay between the time set by the user and when didReceiveLocalNotification: is called. 38 seconds to be exact.
I may just be nitpicking, but it's killing me that I can't figure out why. I went out searching through GitHub to find some other examples, and they all seem to be following the same pattern as I am.
What might cause a delay like this? If this is normal behavior, what can be done to get rid of the delay?
Please let me know if any of my code might be helpful. As didReceiveLocalNotification: is a predefined method, and I have no control over when it is called, I'm not sure what code you might need.
Additional Information
Method for setting alarm time
- (void) scheduleLocalNotificationWithDate: (NSDate *) fireDate
{
UILocalNotification *backgroundNotification = [[UILocalNotification alloc] init];
backgroundNotification.fireDate = fireDate;
backgroundNotification.alertBody = #"Wake Up!";
backgroundNotification.soundName = #"My_Alarm.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification: backgroundNotification];
}

Resources