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.
Related
I have an app which must report user location even if it backgrounded or even killed (terminated). The issue is that the app should report the location not rare than 1 hour interval.
I'm using significant location change (SLC) to track all movements which is quite enough when user is on the go but once user stops no further updates is raised and the app has no opportunity to submit new location (while user stays in the same area but without SLC).
To cover this case I start to use Background fetches to periodically send updates location in background even without SLC). The issue here is that background fetches used to work quite often (every 20-30 min since I used then in another iOS 7.x app) but now with iOS8 / iOS9 I can get it only once a day or so which is not acceptable in my case. I have executed ton of tests, developed simple background fetch app which add a local notification on fetch. No luck to force it to work more often.
Here is my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
return YES;
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
localNotification.fireDate = now;
localNotification.alertBody = [NSString stringWithFormat:#"Background fetch!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
NSInteger number = [UIApplication sharedApplication].applicationIconBadgeNumber;
number++;
localNotification.applicationIconBadgeNumber = number;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
completionHandler(UIBackgroundFetchResultNewData);
}
All what is done here is to add local notification on every background fetch. I always finalize the background execution with UIBackgroundFetchResultNewData.
Do you have any suggestions on how to force background fetches to work more often (or prove links that it is not possible any more)?
Any alternative solutions to meet my requirement are also welcomed!
It turned out that background fetches in iOS depend a lot on what you are doing within the handler, especially network activity. Here the list of dependencies which you should consider trying to understand if and how often iOS will execute your fetch:
time you spend in handler
result (NoData, NewData)
error handling (you will be launched less likely if you code crashes
timeouts (your code execution could be interrupted by iOS)
power usage
network activity related to result (you MUST do a network request when saying that you have NewData, otherwise your fetch could be executed next time in a day or so never.
Apps that download small amounts of content quickly, and accurately
reflect when they had content available to download, are more likely
to receive execution time in the future than apps that take a long
time to download their content or that claim content was available
but then do not download anything.
The last item turned out to be crucial in my case, because for testing purposes I declared fetches and never tried to download anything. Once I started to use network in the handler, background fetches continue to work as expected every 15-30 minutes.
Apple docs:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
I worked on a similar project. After trying various solutions, the only way I've found is the solution proposed here:
https://github.com/voyage11/Location
Here is the tutorial corresponding to the git:
http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
All credits to https://stackoverflow.com/users/1995940/ricky
It's saved me a lot of time!
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.
I'm try to build a messaging apps, with push notification applied. I wish to apply user defined notification tone, like whatsApps does. I notice whatsApps is using IOS default sound system, and i google around, there is a method to get system sound, by accessing "/System/Library/Audio/UISounds". However, i notice the list returned is not same as the list in system "Setting->sound".
Here is my question
1) how am i possible to get all the sound file, is good with the naming same as IOS setting.
2) If i wanted to apply those sound in push notification, do all the .caf file need to include in my apps bundle?
3) is yes, is it possible i able to download it over internet?(i've google a lot and found no luck) or any way to convert/export it?
you just need to set server side payload json like following:
{
aps = {
alert = "Hello World";
badge = 1;
sound = default;
};
}
sound = default; that alert tone apply default one
For second question yes you have to put all sound file in to your apps bundle.
And do not forget to set this following code in to your Appdelegate class when you registerForRemoteNotificationType :
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Here it is link for iPhone sound files list.
I keep reading the documentation and is just not making sense. So i decided to ask the question. Applications on the Iphone like the clock which has an alarm on it. When i set the alarm and close the application the application still notify's me at 4:30am eventhough i have closed the application. Now my understanding is that in order for something like this to work you would have to have it running on the background at all times. This is the part that does not make sense. If i completely shut down the app by double clickin my home button and then swiping up to get rid of the app, how does the app monitor time if is no longer running in the background? Apple states that their are 5 states
1-NOT RUNNING
2-Inactive
3-Active
4-Background
5-Suspended
explanation of the above are located here
i would imagine that when i shut down an application the state is not running. However the alarm application still comes on. HOW? this is killing me. Is there a special state that only native apps can have?
Any information or further understanding would be greatly appreciated.
This is not the case Miguel. What you are looking for is:
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html
Using UILocalNotifications, your app does not have to keep track of time at all. It schedules a notification, and the operating system keeps track of it. I have built quite a few apps with notifications/alarms. I hope this is helpful.
You can use Local notifications.
iOS will notify user when your previously added local notification fires. [iOS will add notification but your app won't be running at all. When user will tap on the notification then the app will open.]
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:#"Hello world"];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}
Tutorial:
http://www.appcoda.com/ios-programming-local-notification-tutorial/
http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/
Concept guide:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html
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];
}