Local notification at multiple times like alarm in ios - ios

I am creating an alarm app. I want user set time and Local notification come at that time. For this i am using following code:-
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = _datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"This is the Alert-Body Text";
localNotif.alertAction = #"Button-Text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
But the problem is notification comes only once and how i set multiple times for local notification so that it comes for every alarm.

Related

UILocalNotification launched every minute util user click it

If I set my notification like the code below, it would launch alarm every minute. However, it gets triggered even my alarm is set behind the current time. For example, the current time is 12:00 and the alarm time is 9:00, it still gets triggered because kCFCalendarUnitMinute is a loop.
How can I get the function as my title description?
UILocalNotification *localNotification = [UILocalNotification new];
localNotification.userInfo = #{#"status":#"alarm",#"note":noteString,#"index":#(index)};
localNotification.fireDate = dateToFire;
localNotification.alertTitle = #"Alarm";
localNotification.alertBody = #"Wake Up!";
localNotification.soundName = #"myTone.m4a";
localNotification.alertAction = #"View";
localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.regionTriggersOnce = true;
localNotification.applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.timeZone = [NSTimeZone systemTimeZone];
// To set the local notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Showing notifications while app in background

I need to show certain local notifications while app is in the background. How can I do it, without the help of NSNotificationCenter?
Andrey Chernukha in first comment is right.
Here is the code to implement simple Local Notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = 0;
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:10];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Test local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Difference between push notification and nsnotifcation centre in iOS sdk

I have question about push notification and the Notification Center. I have built an app and now I want to support notification the the time hits 0 I'm doing like countdown app shows the release date for games and title.
Should I use nsnotifcation center or push notification
You dont need Push Notifications you can acheive that with UILocalNotification. For Push Notifications you need coordination with server but for UILocalNotification you can do it inside your application so better try to use UILocalNotification
For simple UILocalNotification
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
For UILocalNotification with timers
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
you can study in detail in here http://www.appcoda.com/ios-programming-local-notification-tutorial/

Local Notification set fire time

I want to set the daily alarm on the bases of user input. Like user will select time from date picker "10:30", Then i need to set alarm at a that time daily. I write the following code:
func setAlarmAtTime(#time:NSString, withMessage message:NSString){
var loacalNotification = UILocalNotification();
var calendar = NSCalendar.currentCalendar();
calendar.timeZone = NSTimeZone.localTimeZone()
var components = calendar.components(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: NSDate.date());
NSLog("%#",NSDate.date())
NSLog(time);
var timeComponents = time.componentsSeparatedByString(":");
components.hour = timeComponents[0].integerValue;
components.minute = timeComponents[1].integerValue;
if components.isValidDateInCalendar(calendar){
var fireDate = calendar.dateFromComponents(components);
NSLog("%#",fireDate!);
loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
loacalNotification.timeZone = NSTimeZone.localTimeZone();
loacalNotification.fireDate = fireDate;
loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
loacalNotification.soundName = UILocalNotificationDefaultSoundName;
loacalNotification.alertBody = message;
}
But it shows different time based on time zone following are the behaviour when i try to set alarm at 6:40:
Current Date: 2014-08-12 12:07:21 +0000
Alarm Time: 6:40
Fire Date: 2014-08-12 01:10:00 +0000
I tried to set time zone to local as well as current but nothing works :(
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *reminderDate = [df dateFromString:self.lblDate.text];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = reminderDate;
localNotification.alertBody = self.txtName.text;
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName =#"sound.mp3";
NSMutableDictionary *notifyDict = [[NSMutableDictionary alloc] init];
[notifyDict setValue:self.lblType.text forKey:NOTIFICATION_TYPE];
[notifyDict setValue:self.txtName.text forKey:NOTIFICATION_TITLE];
if (![self.tvDescription.text isEqualToString:#"Write Description"]) {
[notifyDict setValue:self.tvDescription.text forKey:NOTIFICATION_DESCRIPTION];
}
[notifyDict setValue:self.lblDate.text forKey:NOTIFICATION_DATE];
localNotification.userInfo = notifyDict;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
The time that it is given, is in greenwich mean time (GMT)
we just need to set
localNotification.timeZone = [NSTimeZone systemTimeZone];
as it will represent your device or system time zone and schedule your app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Register for Local Notification

I am developing an IOS application with phonegap and need to set local notification for it which will repeat on every friday and specified time
Also there is requirement that user will decide to receive or not the local notification
I recommend you read the following article on the topic which i found very helpful
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Body";
notif.alertAction = #"AlertButtonCaption";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
This is just a basic outline of how it works but starting from this you should be able to schedule a notification.

Resources