Alarm app in objective C sound - ios

I am developing an app that will fire an alarm and play 5 secs of a song the user select, or the default sound every x time depending on the user input.. my problem is I do not know how to create this alarm, I have tried with UILocalNotification, but the problem is how I made the alarm sound again even if my application is on background?
here is what I've got so far:
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate * secs = [[NSDate alloc] initWithTimeIntervalSinceNow:i*[secs doubleValue]];
notification.alertBody = #"Alarm";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.fireDate = secs;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
but how could I make the notification and also, how to make the sound play a user song even in background... I have also tried with NSTimer but it stops when the app is moved to background

Use NSTimer when the app is in the foreground.
Look at the repeatInterval property of UILocalNotification for running in the background.

Related

Save Local Notification in ios

How to save the local notification? I want to use the local notification fire date again to schedule local notification.
You should use UILocalNotification
Please check out documentation: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/
It look something like:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"Alert Fired at %#", dateTime];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
About notification re-schedule cycle:
Firing the notification is not enough to show up another one. Your app won't be launch except if user tap the notification.
You could pre-schedule a notification for every day or week for the next month and unschedule them if user open one. Depending of your need you could also fire a remote notification with a content-available key which will launch your app in background and allow you to reschedule a new local notification.

UILocalNotification with repeat-once behaviour like in Messages App

I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

How to register Local Notifications when app is in background or closed IOS

I am working on a Contact manager app in which i have to schedule birthday notifications of contacts that have birthday on same day or one day before the current date. Can i do it in background or when my app is not running.
Is it possible? If Yes then give some helpful links or blogs.
Thanks
You cannot register for local notifications when you are in background.
Use the below code to register for local notification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Your text";
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:120.0]; // your date
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
When your are in foreground, your application:didReceiveLocalNotification will be called.
When you are in background or your app is terminated, you will get the local notification in Notification panel of the device

iOS offline post a toast from background

I'm not sure if an iOS app can post a notification from the background without internet connection? (so this is not a push notification, just to post from phone)
Example: An iOS app that plays music in background can prompt user how long the user has listened to music from background.
I think you should use local notification, please see the below code for local notification,
NSDate *alertTime = [[NSDate date]
dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"bell_tree.mp3";
notifyAlarm.alertBody = #"Staff meeting in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}
With these requirements in the mind, the following code creates an NSDate object based on the current date and time plus 10 seconds. This date object is then used to schedule a notification with no repeats, a text message and the sound from the audio file
The local notifications in iOS can help you to fix the problem. May be this tutorial helps you.

How do I cancel an iOS local notification after a particular time?

I want to cancel an iOS local notification after a particular time.
For example : a week later
- (void)ViewDidLoad
{
NSDate *date = [NSdate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
//set some localNotif's properties
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.fireDate = date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
How can I cancel localNotif after a week (7 days) and can you show me the code?
You can't do that unless your app is running in the foreground at the time the notification is due to be cancelled (in which case there would be no need to cancel it anyway).
The reason you can't do it is because you would need a timer to tell you when to cancel it, and you can't schedule a timer unless you are an app that has a background mode, in which case you could schedule a timer to notify you - but even background apps can be suspended still it would not be guaranteed.
See here
iOS Run Code Once a Day

Resources